5

Hi so I'm trying to export an image such as the one below: enter image description here

Given by the code:

\begin{align*}
    \text{Solving: } 5^{n+2}&=8:\\
    \log_5\left(5^{n+2}\right)&=\log_5(8),\\
    n+2&=\log_5\left(2^3\right),\\
    n&=3\log_5(2)-2\approx \boxed{-0.708}.\\
\end{align*}

I got that image using the snipping tool, but I was wondering if there is a good way to just generate this image as a standalone image by latex? I'm not quite sure. I'm really new to latex so any help would be appreciated! I mainly use overleaf, so if anyone knows how to do this for overleaf that would be great!

Was told to specify OS: Windows 11.

Oh and finally, just a side, if anyone has suggestions for how to better format that solution, that would be great too. I thought my align way was pretty good, but it does shift to the right a bit and look a bit weird. Any suggestions appreciated!

jackson
  • 387
  • 3
    You can give the standalone documentclass a try. Otherwise, you probably should specify which OS you're working with, as some external tools aren't available on every plateform (I'm thinking about the excellent LaTeXit, which is macOS only). – Miyase Jun 06 '22 at 02:12
  • Thanks, @Miyase! I'm on W11 currently, so not macOS D: – jackson Jun 06 '22 at 02:15
  • I don't really know how to use the standalone documentclass, how do I get it to generate an image not a pdf, and how does the standalone document class really work? Also, is there a simpler editor to use than overleaf, because overleaf is complicated as I have to make an entirely new document, etc. Also it has a document limit! – jackson Jun 06 '22 at 02:16
  • 1
    Basically: LaTeX generates DVI or PDF files. If you want to create image formats, you have to use some additional tool to convert the output to the new format. The conversion can be done by dvipng, pdftoppm, or the convert command from ImageMagick, just to name a few options. – Willie Wong Jun 06 '22 at 02:31
  • To do it in a single step you are basically required to either write your own shell/batch script to automate the above process, or use something that other people have written. LaTeXit is venerable. For a cross platform solution, try klatexformula. – Willie Wong Jun 06 '22 at 02:33
  • Thanks for the help. I looked at the first link, and this: \documentclass[preview]{standalone} does what I want to do except it doesn't shorten the horizontal distance, only vertical. Also it's still not a png, just a pdf. I tried downloading klatex but it gives me an error about not being able to query version of Ghostscript located at `'. – jackson Jun 06 '22 at 02:38

2 Answers2

4
\documentclass[border=0pt,12pt]{standalone}
\usepackage{amsmath}
\begin{document}
\minipage{42mm}
Solving $5^{n+2}=8$.
\begin{align*}
\log_5\left(5^{n+2}\right)
    &=\log_5(8)\\
n+2
    &=\log_5\left(2^3\right)\\
n
    &=3\log_5(2)-2 \\
    &\approx \boxed{-0.708}
\end{align*}
\endminipage
\end{document}

enter image description here

If you need a border, change border=0pt to border=12pt. Here is the result. The magic number 42mm is obtained by trial and error.

enter image description here

Note that the red borders are intentionally added when converting the PDF output to PNG. Here is the Windows command to execute ImageMagick's convert. If you use Linux or others, you might not need magick prefix.

magick convert -compose copy -bordercolor red -border 1x1 -density 200 -alpha remove "filename.pdf" "filename.png"

Converting multipage PDF to a series of PNG images

  1. You have to install ImageMagick and make sure that it has been registered to the PATH environment variable as follows: enter image description here

  2. Use the following LaTeX template whenever you want to create a bunch of PNG images. Save it as template.tex in a folder called xyz. The compilation will be done in the 3rd step below.

    \documentclass[border=12pt,12pt,multi,preview]{standalone}
    \usepackage{amsmath}
    \begin{document}
    \preview
    \minipage{42mm}
    Solving $5^{n+2}=8$.
    \begin{align*}
    \log_5\left(5^{n+2}\right)
        &=\log_5(8)\\
    n+2
        &=\log_5\left(2^3\right)\\
    n
        &=3\log_5(2)-2 \\
        &\approx \boxed{-0.708}
    \end{align*}
    \endminipage
    \endpreview
    

    \preview \minipage{42mm} In Vladimir Putin We Trust \begin{align} E &\not= mc^2 \ pV &\not= nRT \end{align} \endminipage \endpreview \end{document}

  3. To make our life easier, it is a good idea to create a batch file generator.bat and save it in xyz.

    echo off
    

    rem %1 PDF filename without extension rem %2 density rem %3 border color

    if exist "%~1.pdf" del "%~1.pdf"

    if exist "%~1.tex" pdflatex "%~1.tex"

    if exist "%~1-.png" del "%~1-.png"

    if exist "%~1.pdf" magick convert ^ -compose copy ^ -bordercolor %3 ^ -border 1x1 ^ -density %2 ^ -alpha remove ^ "%~1.pdf" "%~1-%%02d.png"

    for %%x in (aux out log toc nav snm) do (if exist "%~1.%%x" del "%~1.%%x")

  4. Open terminal (by pressing Win+R, typing cmd and pressing enter), change the active directory to xyz, and type the following:

    generator.bat template 200 red
    
  5. You will get a bunch of PNG images.

Display Name
  • 46,933
  • I intentionally rearranged your layout and removed the unnecessary punctuations. Everything should be made as simple as possible, but not simpler. – Display Name Jun 06 '22 at 04:50
  • Thanks so much for your help and for helping with formatting! Why is the punctuation unnecessary? I see in textbooks they have commas after lines, etc. Also, I'm not quite sure how ImageMagick works, where would I be able to learn more about it? Thanks for this though, it was great! – jackson Jun 06 '22 at 04:59
  • 1
    @jackson: See my last edit. – Display Name Jun 06 '22 at 05:34
3

This mostly takes care of it. align* and flalign* seem to want to center the alignment point. There was an odd gap on the right which was due to an extra &. Thanks to Mico for finding it.

\documentclass[varwidth]{standalone}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\begin{aligned}
    \text{Solving: } 5^{n+2}&=8:\\
    \log_5\left(5^{n+2}\right)&=\log_5(8),\\
    n+2&=\log_5\left(2^3\right),\\
    n&=3\log_5(2)-2\approx \boxed{-0.708}.\\
\end{aligned}
\end{equation*}
\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • The "odd gap on the right, which seems to come from \end{aligned}" actually comes from the specious & symbol at the end of the first of four rows of output. I would also suggest replacing both \begin{equation*} and \end{equation*} with $ and getting rid of the double-backslash directive at the end of the fourth row. – Mico Jun 06 '22 at 04:44
  • Thanks, but how can I get this to a png form? – jackson Jun 06 '22 at 05:00
  • @Mico just curious, why does the & add that space? Also, replacing with a $ cuts off the 2 from the n+2 in the power at the top. – jackson Jun 06 '22 at 05:01
  • You can use a graphics editor like GIMP. I purchased Acrobat some years ago and simply use save as while viewing. – John Kormylo Jun 06 '22 at 14:04
  • @JohnKormylo - Sorry for that. There's no error message if I omit the varwidth option on my Mac (MacOS 12.4, MacTeX2022); however, there's definitely an error message when I run the test document under MikTeX. I've deleted the earlier comment. – Mico Jun 06 '22 at 14:28