Skip to content

Instantly share code, notes, and snippets.

@retorillo
Last active February 22, 2024 10:51
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save retorillo/4ed72e705bdb6e3e2c6c29cada29f012 to your computer and use it in GitHub Desktop.
Save retorillo/4ed72e705bdb6e3e2c6c29cada29f012 to your computer and use it in GitHub Desktop.
LaTeX to PNG on Windows

LaTeX to PNG on Windows

Prerequisite

Workaround 1: imgconvert command

imgconvert is not installed by default Chocolatey installer. So, you must create batch file on PATH as follows:

REM imgconvert.bat
magick convert %*

NOTE: On Unix System, convert is used by default. (On Windows, name of convert is registered by legacy name of drive manager)

Workaround 2: gswin32c command

choco install ghostscript.app --x86 DOES NOT add executables into PATH. Therefore, must add them by your hands.

If you already have x64 binaries and don't want to additionally install x86, the following workaround may work: (not tested)

REM gswin32c.bat
REM Workaround: Bypassing to x64 binaries
gswin64c %*

Or, use gsexe option.

the Ghostscript executable is usually named ‘gs’ under Linux/U-nix and ‘gswin32c’ under MS Windows and configured this way by default, butthis may be changed using the gsexe setting (5.6.2 Conversion Software)

Step 1: Write code

\documentclass[
  11pt,
  border=2,
  convert={
    density=300,
    outext=.png
  },
]{standalone}
\begin{document}
  $f(t) = 4k(\frac{t}{D})^{3}$
\end{document}
  • border can used as "padding"
  • density affects its output resolution

Step 2: Compile

pdflatex --shell-escape --enable-write18 file.tex

The above command should produce file.pdf and file.png.

latex2png_win_result.png

  • In modern pdflatex seems to enable --enable-write18 by default. So this may be not mandatory.
  • NOTE: that options must be preceding by filename. eg. pdflatex file.tex --shell-escape ... goes failed.

Injecting command line option into Image Magick

According to 5.6.3 Conversion process Table 2: Advanced Conversion Options, pre-expansion command line template as follows:

command=\unexpanded{{\convertexe\space -density \density\space
\infile\space \ifx\size\empty\else
-resize \size\fi\space -quality 90 \outfile}}

Therefore, \density can be used as good placeholder command line options. eg.

\documentclass[
  convert={
    density=300 -alpha off -background #ffffff,
    outext=.png
  },

See Also

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