Skip to content

Instantly share code, notes, and snippets.

@airglow923
Last active June 10, 2021 09:55
Show Gist options
  • Save airglow923/46f8c9bb89c1ba49f093dc9918b7a819 to your computer and use it in GitHub Desktop.
Save airglow923/46f8c9bb89c1ba49f093dc9918b7a819 to your computer and use it in GitHub Desktop.
Convert LaTeX code into URI so as to embed it in websites
const latex = Deno.args[0];
const latexRenderer = 'https://latex.codecogs.com/svg.latex?';
if (latex === undefined || typeof latex !== 'string') {
const errMsg = 'Invalid argument';
writeStdStream(errMsg, Deno.stderr);
Deno.exit(1);
}
async function writeStdStream(str, stream = Deno.stdout) {
return stream.write(new TextEncoder().encode(str));
}
function asciiToUTF(str) {
str = str.replace(/\(/g, '%28');
str = str.replace(/\)/g, '%29');
return str;
}
const encodedLatex = encodeURI(latex);
const normalizedLatex = asciiToUTF(encodedLatex);
writeStdStream(latexRenderer + normalizedLatex + '\n');
const latex = process.argv[2];
const latexRenderer = 'https://latex.codecogs.com/svg.latex?';
if (latex === undefined || typeof latex !== 'string') {
const errMsg = 'Invalid argument';
process.stderr.write(errMsg);
process.exit(1);
}
function asciiToUTF(str) {
str = str.replace(/\(/g, '%28');
str = str.replace(/\)/g, '%29');
return str;
}
const encodedLatex = encodeURI(latex);
const normalizedLatex = asciiToUTF(encodedLatex);
writeStdStream(latexRenderer + normalizedLatex + '\n');
#!/bin/sh
CURRENT_DIR=$(dirname $0)
COMMAND=""
usage() {
# additional error messages
if [ "$#" -ne 0 ]; then
printf "$@\n" >&2;
fi
printf "Usage: $0 LATEX\n" >&2;
}
if [ "$#" -eq 0 ]; then
usage
exit 1;
fi
if command -v deno > /dev/null; then
COMMAND="deno run $CURRENT_DIR/deno-to-latex-uri.js"
elif command -v node > /dev/null; then
COMMAND="node $CURRENT_DIR/node-to-latex-uri.js"
else
usage "No JavaScript runtime found"
exit 1;
fi
$COMMAND "$1"
@airglow923
Copy link
Author

airglow923 commented May 24, 2021

Usage:

  1. Place deno-to-latex-uri.js, node-to-latex-uri.js, and to-latex-uri.sh in the same directory.
  2. Run chmod +x to-latex-uri-.sh
  3. ./to-latex-uri.sh YOUR_LATEX

Example:

foo@bar:~$ ./to-latex-uri.sh '\huge
\begin{align*}
  -wtx =
  \begin{cases}
    TMinw, & \text{if}\ x = TMinw \\
    -x, & \text{if}\ x > TMinw
  \end{cases}
\end{align*}'

Results in

https://latex.codecogs.com/svg.latex?%5Chuge%0A%5Cbegin%7Balign*%7D%0A%20%20-wtx%20=%0A%20%20%5Cbegin%7Bcases%7D%0A%20%20%20%20TMinw,%20&%20%5Ctext%7Bif%7D%5C%20x%20=%20TMinw%20%5C%5C%0A%20%20%20%20-x,%20&%20%5Ctext%7Bif%7D%5C%20x%20%3E%20TMinw%0A%20%20%5Cend%7Bcases%7D%0A%5Cend%7Balign*%7D

This can be used as follows:

![](https://latex.codecogs.com/svg.latex?%5Chuge%0A%5Cbegin%7Balign*%7D%0A%20%20-wtx%20=%0A%20%20%5Cbegin%7Bcases%7D%0A%20%20%20%20TMinw,%20&%20%5Ctext%7Bif%7D%5C%20x%20=%20TMinw%20%5C%5C%0A%20%20%20%20-x,%20&%20%5Ctext%7Bif%7D%5C%20x%20%3E%20TMinw%0A%20%20%5Cend%7Bcases%7D%0A%5Cend%7Balign*%7D)

This shows

To simplify the execution, one can

  1. Add the directory the shell scripts are in to PATH; or
  2. Add a function to a shell configuration file (ex. .bashrc, .zshrc) that executes the script.

In my case, I added the following lines to .zshrc:

to-latex-uri() {
  $SCRIPTS/to-latex-uri.sh "$@"
}

and it can be called without the extension (.sh):

foo@bar:~$ to-latex-uri '\huge
\begin{align*}
  -wtx =
  \begin{cases}
    TMinw, & \text{if}\ x = TMinw \\
    -x, & \text{if}\ x > TMinw
  \end{cases}
\end{align*}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment