I've installed Tex2png, a PHP-based solution to produce png math images.

Simple expressions render fine, but some environments do not (with unhelpful "LaTeX error" messages).

It seems my Linux server had very few packages. I unzipped these:

  1. amsmath
  2. amscls
  3. tools



This got some expressions to at least render something, but not correctly. For example, this:

\begin{aligned}\text{Vol}&=\pi\int_1^b{\frac{1}{x}}^2dx\\ &=\pi\int_1^b{x^{-2}}dx\\ &=\pi\left[-\frac{1}{x}\right]_1^b\\ &=\pi\left(1-\frac{1}{b}\right)\end{aligned}

gives this (partial screen shot), with everything horizontal:

horizontal equation

I've tried \align, \align*, \aligned and \aligned*, but they either give "LaTeX error" or the vertical alignment is ignored. So presumably I'm missing a package or two, but I thought amsmath and tools should handle most of these cases.

I don't know:

  1. Which packages are already on the server
  2. Whether I've unzipped the packages to the right places
  3. Which packages I need add to address the above

Any pointers would be very welcome.

  • Align etc. are all environments. While environments are implemented using macros (\align and \endalign), you need both. Note that amsmath and mathtools come with manuals on their use. – John Kormylo Sep 25 '18 at 3:38
  • Thanks, John. I know enough LaTeX to be dangerous - not enough yet to master it. All working OK now. – MBourne Sep 25 '18 at 7:34

This isn't quite an answer to your question. I don't have or know much about Tex2png. It seems to me that perhaps Tex2png is limited in the extent to what sorts of environments it can handle. Namely, it looks like it doesn't handle math environments very well.

But, you can create something similar just running pdflatex. Here's an example:


  \text{Vol}&=\pi\int_1^b{\frac{1}{x}}^2dx      \\ 
            &=\pi\int_1^b{x^{-2}}dx             \\ 
            &=\pi\left[-\frac{1}{x}\right]_1^b  \\ 


which produces:

enter image description here

Granted, this will be a pdf document. Perhaps you can use it as is. But, if you actually need to work with a png, there are various tools available that will allow you to convert from one kind of format to another.

The provided solution to How to output a multiline equation with no margins may be of interest to you.

The solution to Compile a LaTeX document into a PNG image that's as short as possible also talks about how to convert between file formats.

Using other environments

If you want to use other environments like align, then you can still do something similar, but you will need to somehow pass align a page width to work with. That can be achieved by using the preview option passed to the documentclass:


Since you're likely to get an image much wider than you want with this approach, look at the answer provided in the first link suggested above.

However, there is a work around. You could put the align environment within a minipage and do something like the following


  \text{Vol}&=\pi\int_1^b{\frac{1}{x}}^2dx      \\ 
            &=\pi\int_1^b{x^{-2}}dx             \\ 
            &=\pi\left[-\frac{1}{x}\right]_1^b  \\ 


I believe the minipage width here is not really a concern if all you're going to do is create the environment for displaying mathematics.

Now, there is some extra code here that makes this look a bit complicated. Namely, the code starting from \makeatletter. This is to get around a peculiar issue in which the align* environment expects it's being called midparagraph. Therefore, align* adds extra whitespace at the top of the environment. For details about this workaround see egreg's solution to Spurious space above align environment at top of page/minipage.

  • Thank you for your detailed answer. There were so many things going wrong it was hard to pin down the root cause and I concluded it was a package problem. Turns out it was 2 issues: Not handling double backslash correctly; and there was an errant extra bracket in my expression. So all is good, finally. Thanks again. – MBourne Sep 25 '18 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.