[Tex/LaTex] Help me to debug long LaTex equations with errors

debuggingtools

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?

enter image description here

Perhaps useful to readers

  1. Help me to write Long LaTeX equations fast with colours and possibly with other aids

  2. How to best debug LaTeX?

Best Answer

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.

enter image description here

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

enter image description here

enter image description here

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

enter image description here

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

enter image description here

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.

enter image description here

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:

enter image description here

\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}
Related Question