Coder Social home page Coder Social logo

bartp5 / libtexprintf Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 2.0 266 KB

Library providing printf-style formatted output routines with tex-like syntax support.

License: GNU General Public License v3.0

Makefile 0.71% C 83.87% M4 0.27% Shell 8.84% Roff 5.52% Awk 0.79%
ascii-art ascii-graphics unicode tex

libtexprintf's Introduction

libtexprintf & utftex

CI

The utftex program and underlying libtexprintf library provide tools to pretty print math in mono-space fonts, using a tex-like syntax. It produces UTF-8 encoded text. The program was inspired by asciiTeX, and the improved asciiTeX fork. However, utftex supports much more TeX syntax and contains extensive Unicode tables to map latex commands to Unicode symbols. Use libtexprintf/utftex to unlock the math capabilities of Unicode in mono-space text applications.

Note that how the equations look depends strongly on the font you use. Naturally, one needs a monospace font with good Unicode support for the symbols you use. A good monospace font for math is, for example,
JuliaMono.

libtexprintf

The core library to pretty print math in mono-space font. Use it to enable your c or c++ code to print out formatted math. The calls are modeled after the printf family to produce formatted output.

utftex

The utftex program uses libtexprinf to implement a command-line utility to format math. For example:

> utftex '\frac{\alpha}{\beta+x}'  

gives

 α   
───  
β+x  

The texprintf library provides a fairly full featured tex-like syntax with arrays, brackets, many symbols, and even font style (note that texprintf outputs UTF-8 and as such cannot change the fonts. However, Unicode does provide several font-styles like mathematical calligraphic style, or italic). For example:

> utftex -F mathnormal '\frac{1}{a+x}' 

gives

 1
───
𝑎+𝑥

It also supports some diacritical combining marks, to do things like

utftex '\vec x' 

to get

x⃗.

A bit more elaborate example is this small table of Laplace transforms:

> utftex '\begin{array}{|l|l|}  
\hline  
f(t) & \mathscr{L}[f(t)]=F(s) \\\hline  
1 & \frac{1}{s} \\\hline  
e^{at}f(t) & F(s-a) \\\hline  
\delta\left(t\right) & 1 \\\hline  
\delta\left(t-t_0\right) & e^{-st_0} \\\hline  
\int_0^t f(x)g(t-x)dx & F(s)G(s)\hline  
\end{array}'  

┌────────────────┬──────────────┐ 
│f(t)            │ 𝓛[f(t)]=F(s) │  
├────────────────┼──────────────┤  
│                │ 1            │  
│1               │ ─            │  
│                │ s            │  
├────────────────┼──────────────┤  
│ at             │              │  
│e  f(t)         │ F(s-a)       │  
├────────────────┼──────────────┤  
│δ(t)            │ 1            │  
├────────────────┼──────────────┤  
│                │  -st         │  
│                │     0        │  
│δ⎛t-t ⎞         │ e            │  
│ ⎝   0⎠         │              │  
├────────────────┼──────────────┤  
│ t              │              │  
│⌠               │              │  
│⎮  f(x)g(t-x)dx │ F(s)G(s)     │  
│⌡               │              │  
│ 0              │              │  
└────────────────┴──────────────┘  

Note that if the table borders do not properly align you are not viewing the table in monospace fonts.

utfstringinfo

Utility to analyze UTF-8 strings. The utfstringinfo program reads arguments or, if no arguments are provided, stdin and for every single character it finds it shows the UTF-8 byte sequence, the Unicode code-point and, if applicable, the utftex command that can be used to generate the symbol.

Software using libtexprintf/utftex

Several projects use libtexprintf or utftex. Some projects that I am aware of are (in arbitrary order):

  • LibTeXPrintf: a wrapper around libtexprintf to use the library directly from the julia programming language
  • kramdown-rfc (a backend for kramdown) uses utftex to export latex equations to plain text
  • utftex-gui, a simple graphical user interface to utftex (much like the gui that came with asciiTeX)

Questions One Might Ask (QOMA)

  • Why is my equation all misaligned?

    • Make sure you are using mono-space fonts.
  • I am using mono-space fonts, why are my equations still poorly aligned?

    • Most fonts do not support all Unicode characters. Your program may use some variable-width fallback fonts.
  • Why are some symbols are not displayed (properly)?

    • Most fonts do not support all Unicode characters. Perhaps your font engine just gave up? This also often happens when using combining diacritical marks. Find the right set of font engine and fonts to use.
  • Can you recommend a good mono-space symbol font?

    • Best is if the monospace font you use supports lots of symbols by itself. There are various fonts that support a great deal of Unicode. See for example this discussion. As mentioned before I recommend JuliaMono.
    • Make your own out of your favorite (variable-space) fonts! The great "monospacifier" will do that for you. Get it at https://github.com/cpitclaudel/monospacifier
  • How do I specify the fallback fonts in <insert some program/system>?

    • This will depend on your system and/or program you are using. If your system uses fontconfig you may try that to set up an appropriate mono-space fallback font.
  • How do I specify the fallback fonts in with fontconfig?

    • Let us assume you use some mono-space font called "MONO" and we have some symbol mono-space fallback font for it "SYMBOLMONO". Here are the steps:
      1. Install all fonts (e.g. copy font files to ~/.local/share/fonts and run "fc-cache -f -v")
      2. Then create a fontconfig file in ~/.config/fontconfig/fonts.conf It could look something like (replace the font names with the appropriate values):
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
 <alias>
   <family>monospace</family>
   <prefer>
     <family>MONO</family>
     <family>SYMBOLMONO</family>
   </prefer>
 </alias>
</fontconfig>	   

libtexprintf's People

Contributors

bartp5 avatar bkmgit avatar stevengj avatar suavesito-olimpiada avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

libtexprintf's Issues

manpage not up to date

$ utftex "a_{utc} < a_{ntp}"
a    < aₙₜₚ
 utc

In github, you may not be able to see the subscript characters that utftex uses for the "ntp" suffix.
(Maybe just copy/paste it to something else, e.g., pbpaste in terminal.)

Even worse than the interoperability problems here is the uneven handling of "utc" and "ntp" -- of course, I know why this happens, but I'd rather have a way to completely switch off the feature that uses subscript characters.

Switch to math from a text block

Currently libtexprintf does not support switching back to a math environment from a text block. For example:

\text{blah $math$ blah}

renders as

blag $math$ blah

Unicode primes

t' and t'' get rendered as-is using ASCII quotes, but they could be rendered using Unicode prime characters t′ and t″.

Here are several Unicode primes.

Misalignment

target = \left\{
\begin{array}{ll}
cwnd                          &
\text{if } \mathrm{W_{cubic}}(t + RTT) < cwnd \\
1.5 * cwnd                    &
\text{if } \mathrm{W_{cubic}}(t + RTT) > 1.5 * cwnd \\
\mathrm{W_{cubic}}(t + RTT)   &
\text{otherwise} \\
\end{array} \right.

renders as

         ⎧
         ⎪cwnd            if  W     (t + RTT) < cwnd
         ⎪                     cubic
         ⎨1.5 * cwnd      if  W     (t + RTT) > 1.5 * cwnd
target = ⎪                     cubic
         ⎪W     (t + RTT) otherwise
         ⎩ cubic

the target = bit isn't vertically centered. Mathjax renders as follows:
Screenshot 2023-01-31 at 13 50 29

Sum not vertically centered

utftex '\\sum_{i=6}^{10}\\left[\\binom{A}{i} \\binom{L}{10-i}\\right]
produces

10
⎯⎯
╲  ⎡⎛A⎞ ⎛ L  ⎞⎤
╱  ⎢⎜ ⎟ ⎜    ⎟⎥
⎺⎺ ⎣⎝i⎠ ⎝10-i⎠⎦
i=6

Also, the bottom of the sum (⎺⎺) is using a different dash than the top (⎯⎯)?

Use Unicode subscripts/superscripts where available?

Currently t_0 renders as

t
 0

However, Unicode has a 0 subscript character, so it could render more cleanly as t₀.

In general, if a subscript or superscript consists entirely of characters available as such in Unicode, it would be nice to render them that way (with the current behavior as a fallback for characters lacking Unicode sub/superscripts).

The available Unicode superscripts are ²³¹ʰʲʳʷʸˡˢˣᴬᴮᴰᴱᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾᴿᵀᵁᵂᵃᵇᵈᵉᵍᵏᵐᵒᵖᵗᵘᵛᶜᶠᶻ⁰ⁱ⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾ⁿⱽꜝ and the subscripts are ᵢᵣᵤᵥ₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎ₐₑₒₓₕₖₗₘₙₚₛₜⱼ

(In general, there are a lot of Unicode characters that you could potentially be using. See https://github.com/JuliaLang/julia/blob/master/stdlib/REPL/src/latex_symbols.jl for a list mapping them from LaTeX and LaTeX-like \symbols.)

New release?

There are several commits that are not yet in a new release. Is it worth to make a new release with them?

LaTeX token don't work correctly

Stuff like \frac 1\alpha or \frac {1}\alpha, which is valid LaTeX math produce errors:

❯ utftex '\frac 1 \alpha'

ERROR: Too few mandatory arguments to command (1x)
❯ utftex '\frac{1}\alpha'
Error, string is not proper UTF-8 code
Error, string is not proper UTF-8 code
1
─

v1.18 build failure on Linux & macOS

Hello 👋 . I'm a maintainer for the Homebrew project. While packaging v1.18 of utftex we are encountering a build failure:

  make[1]: Entering directory '/tmp/utftex-20230524-5877-efo77/libtexprintf-1.18/src'
  /bin/bash ./gen_errorflags.sh ./boxes.c ./drawbox.c ./lexer.c ./parser.c
  /bin/bash ./gen_errorflags.sh ./boxes.c ./drawbox.c ./lexer.c ./parser.c
  Collecting error flags from ./boxes.c
  Collecting error flags from ./boxes.c
  Collecting error flags from ./drawbox.c
  Collecting error flags from ./drawbox.c
  Collecting error flags from ./lexer.c
  Collecting error flags from ./lexer.c
  Collecting error flags from ./parser.c
  Collecting error flags from ./parser.c
  rm: cannot remove 'tmperrflags': No such file or directory
  make[1]: *** [Makefile:1079: errorflags.h] Error 1
  make[1]: Leaving directory '/tmp/utftex-20230524-5877-efo77/libtexprintf-1.18/src'
  make: *** [Makefile:674: install-recursive] Error 1

The relevant GitHub Actions run can be found here.

Relates to Homebrew/homebrew-core#131899

Create a target for library

First, nice project! I think it would be cool to have a target on the Makefile for building as a library to be able to link to. I would like to help, but I don't know about automake neither autoconf.

Gui for utftex

I made a GUI using PyQt6. It should be easily translatable to PySide6. It borrows a lot of ideas from AsciiTeX.

It's designed to be made into an executable file with PyInstaller.

utftexgui.zip

1.17 building fails

aclocal: warning: couldn't open directory 'm4': No such file or directory
glibtoolize: putting auxiliary files in '.'.
glibtoolize: copying file './ltmain.sh'
glibtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'.
glibtoolize: copying file 'm4/libtool.m4'
glibtoolize: copying file 'm4/ltoptions.m4'
glibtoolize: copying file 'm4/ltsugar.m4'
glibtoolize: copying file 'm4/ltversion.m4'
glibtoolize: copying file 'm4/lt~obsolete.m4'
glibtoolize: Consider adding '-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
configure.ac:3: warning: The macro `AC_PROG_LIBTOOL' is obsolete.
configure.ac:3: You should run autoupdate.
m4/libtool.m4:100: AC_PROG_LIBTOOL is expanded from...
configure.ac:3: the top level
configure.ac:5: warning: The macro `AM_ENABLE_SHARED' is obsolete.
configure.ac:5: You should run autoupdate.
m4/ltoptions.m4:205: AM_ENABLE_SHARED is expanded from...
configure.ac:5: the top level
configure.ac:3: installing './compile'
configure.ac:3: installing './config.guess'
configure.ac:3: installing './config.sub'
configure.ac:7: installing './install-sh'
configure.ac:7: installing './missing'
configure.ac:48: error: required file 'test/mathjax_examples.tex' not found
configure.ac:70: error: required file 'test/mathjax_examples.ref' not found
configure.ac:93: error: required file 'test/mathjax_examples-A.ref' not found
parallel-tests: installing './test-driver'
src/Makefile.am: installing './depcomp'
autoreconf: error: automake failed with exit status: 1
BuildError: Failed executing: ./autogen.sh 

maintainer-clean fails on mac-os

CI fails on mac-os with the following log:
8_Clean.txt

Not sure why and I have no mac to reproduce. Error first came up after updating the README, so it seems the problem comes from a change in build environment, rather than a change in libtexprintf itself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.