# Help me to debug long LaTex equations with errors

I want to learn debugging of long LaTeX equations like below:

$\tau=\int\frac{dt}{\gamma} = \int\sqrt{1-\frac{v(t)^2}{c^2}}dt = \int\sqrt{1-\frac{1}{c^2}\left(\left(\frac{dx}{dt}\right)^2+\left(\frac{dy}{dt}\right)^2+\left(\frac{dz}{dt}\right)^2 \right) dt$


I have tried to find LaTeX -debugger in places such as iPad's MathBot but MathJax is pretty much the best tool as you can see below and particularly this script here but the tool is only for ready outputs, I press it and it will display in red if something wrong. I would be very happy if I found some tool that put my equation to red in points where I may have error, does such debugging TeX -tool exist?

-
...I feel I am formulating this question wrong, something about debuggers here, investigating. –  hhh Oct 4 '12 at 22:17

Similar to what I wrote in Help me to write Long LaTeX equations fast with colours and possibly with other aids, and what others have wrote here, the only way to debug these really is to break them down and re write them in a clearer manner.

I don't think there is going to be one generic method that will work for all types of problem. But for this specific case, the equation as is yields the following message:

Missing } inserted.

This particular error usually means that the curly braces are not matched. So the next thing I do is to use a feature that I think is available in most LaTeX Editors/IDEs (I know it is in TeXShop and TeXworks), and click on the opening curly brace to get to locate the matching closing curly brace.

So, the first one matches, and so do the next few:

Once you get to the \sqrt after the third equal sign, you find that there is no matching closing brace:

So that tells you where the problem is. Adding the closing brace fixes the syntax, and you are done with the debugging:

So, all that is now left are the cosmetic aspects. The integral sign seems rather small. So you can either load the bigints package as per Big integral sign, but I found the results better with \mathlarger.

## Notes:

• I thought that this equation should be in display mode, but resulted in the large square root being too vertical, which did not look good to me, so left it in inline mode
• Also, not sure why you have braces around the (t), but I left them as is.

Then, adding some new lines and spacing the code to make it more readable we have:

\documentclass{article}
\usepackage{amsmath}

\usepackage{relsize}
\newcommand{\intL}{\mathlarger{\mathlarger\int}}

\begin{document}
$\tau = \intL\frac{dt}{\gamma} = \intL\sqrt{1-\frac{v(t)^2}{c^2}}dt = \intL\sqrt{ 1 - \frac{1}{c^2} \left( \left(\frac{dx}{dt}\right)^2 + \left(\frac{dy}{dt}\right)^2 + \left(\frac{dz}{dt}\right)^2 \right) } dt$
\end{document}


If you are typing these types of equations often, you might want to consider defining macro such as the \D{} below to make your code even more readable and easier to debug. Furthermore, the d in dt should be upright. So with those changes you get:

\documentclass{article}
\usepackage{amsmath}

\usepackage{relsize}
\newcommand{\intL}{\mathlarger{\mathlarger{\int}}}
\newcommand{\dd}[1]{\mathrm{d}#1}
\newcommand{\D}[1]{\left(\frac{\dd{#1}}{\dd{t}}\right)}

\begin{document}
$\tau = \intL\frac{\dd{t}}{\gamma} = \intL\sqrt{1-\frac{v(t)^2}{c^2}}\dd{t} = \intL\sqrt{ 1 - \frac{1}{c^2} \left( \D{x}^2 + \D{y}^2 + \D{z}^2 \right) } \dd{t}$
\end{document}

-
Don't use \mathlarger for that integral: they are small because you're using inline math mode, while such a formula should always be displayed. –  egreg Oct 5 '12 at 6:55
@egreg: Even with display math the integral sings are quite small. The reason I did not put the final image in display math is because of how the second radical sign is displayed. This is something I have not seen before. –  Peter Grill Oct 5 '12 at 6:59

Your forgot to close the square root command \sqrt.

You mean

$\tau=\int\frac{dt}{\gamma} = \int\sqrt{1-\frac{v(t)^2}{c^2}}dt = \int\sqrt{1-\frac{1}{c^2}}\left(\left(\frac{dx}{dt}\right)^2+\left(\frac{dy}{dt}\right)^2+\left(\frac{dz}{dt}\right)^2 \right) dt$


I guess.

-
...I know the solution, I want some tool that I press and it shows some colors perhaps statistic analysis or something to look for possible -error-spots. This kind of errors like missing last parethesis may require many minutes work to find out by investigating each spot separately. –  hhh Oct 4 '12 at 20:45
@hhh TeX's error message ! Missing } inserted. <inserted text> } l.25 told you all there is to debug. A proper editor can highlight the erroneous line and/or jump to it when you click on the error message. –  Qrrbrbirlbel Oct 4 '12 at 20:48
@Qrrbrbirlbel TeX's error message, how did you get it? PdfLatex sounds overkill here -- I usually debug TeX with pdflatex but it require all kind of headers/unnecessary things -- sorry stupid question but I am serious. How do you get this error -msg and highlighting i.e. how do you debug? –  hhh Oct 4 '12 at 20:54
It's in the .log file. How did you now in the first place that you had an error? –  Qrrbrbirlbel Oct 4 '12 at 21:06
@hhh Well, that would have been helpful to know in the first place, that you don't use your local TeX distribution. Try for example an Online LaTeX Equation Editor. Here you can see your results live. The one linked tells you “You have more open '{' brackets than closed '}' brackets”. –  Qrrbrbirlbel Oct 4 '12 at 22:45

I don't know if there is a tool that will do exactly what you desire. Perhaps it is not even possible to have such a tool for the general case (LaTeX being a macro language etc). Having a decent editor with a good LaTeX plugin is a good step in this direction though (colors, highlighting closing braces etc).

Apart from that, it is also helpful to use multiple lines instead of a single long one. Indentation too. This way, you can always comment out a single line at a time. This always helps in pinpointing the error. Look how much more readable your code can be.

$\tau = \int\frac{dt}{\gamma} = \int\sqrt{1-\frac{v(t)^2}{c^2}}dt = \int\sqrt{1-\frac{1}{c^2}} \left(\left(\frac{dx}{dt}\right)^2 + \left(\frac{dy}{dt}\right)^2 + \left(\frac{dz}{dt}\right)^2 \right) dt$


Another technique that I find useful is working in chunks, or more accurately writing the command first and filling the arguments later. E.g.

% first step
\dfrac{}
{}

% second step
\dfrac{numerator}
{denominator}


Defining custom commands also can be very helpful. E.g.

\newcommand{\abs}[1]{\ensuremath{\left|#1\right|}}
\newcommand{\paren}[1]{\ensuremath{\left(#1\right)}}
\newcommand{\brac}[1]{\ensuremath{\left[#1\right]}}
\newcommand{\Brac}[1]{\ensuremath{\left\{#1\right\}}}


Another approach would be to check if there is a decent converter from Word or LibreOffice equations to LaTeX and using it, but this is something that may be rather limiting in the long run since you will probably need to hand tweak the equations (no custom commands etc)

-