13

I have a big bunch of old to new LaTeX documents written by several independent people. I have to bring them up to date, modern and consistent, standardized LaTeX code. In these documents there are a lot of common "mistakes" or quirks. For example:

\begin{equation}
  a = b
\nonumber
\end{equation}

instead of just

\[
  a = b
\]

and similar cases (eqnarray instead of align and so on). Or completly random code linebreaks in text (no output changing breaks, like paragraphs) that could be joined.

What tools are recommended to automate such kind of tasks? Mainly to change code snippets to other code snippets without changing the raw content of the snippet. A find-and-replace that takes care of LaTeX syntax/semantic, so to speak.

Are there such tools? I'm working on Windows.

4
  • 2
    I have used perl to do limited search and replace on certain constructions, but writing regular expressions that cope with all possible idiosyncrasies of TeX is well nigh impossible, so careful hand checking is always necessary. Nov 1, 2012 at 16:39
  • 2
    In the past, I had used gema to translate a 700 page book written in plain TeX to LaTeX (I needed some hand-tuning for figures and tables). Gema is more versatile than regular expressions, has a windows port, and includes an example of tranlating LaTeX to HTML
    – Aditya
    Nov 1, 2012 at 23:04
  • @Aditya: can gema read/write utf-8 files?
    – mbork
    Nov 13, 2012 at 18:28
  • Isn't this question unnecessarily too broad? Jun 26, 2021 at 9:41

3 Answers 3

9

The first step could be loading the nag package at the beginning of each document, which checks for common mistakes, outdated packages and obsolete commands:

\RequirePackage[l2tabu, orthodox]{nag}

Context aware checks and replacements could be made with regular expressions and tools like perl, php, or sed, for example. But you need to know what to look for.

Furthermore helpful: grep, egrep, find, for example:

find "." -name "*.tex" | xargs grep eqnarray

You could extend this to search whole directory trees for a list of undesired patterns.

I would do this on Linux, as there are all mentioned tools usually preinstalled. On Windows, you could install Cygwin (for shell and tools) and ActiveState Perl, for example.

3
  • I already use nag. And I'm using Windows (see question). So grep is not available.
    – Foo Bar
    Nov 1, 2012 at 16:45
  • 2
    @FooBar Please see my update, on Windows much tools are missing but can be installed.
    – Stefan Kottwitz
    Nov 1, 2012 at 16:46
  • Thanks. I did some further reseacrh myself and waited if someone has a better (more easy) solution. But since there was none and I also found nothing myself, your answer should be the correct one.
    – Foo Bar
    Nov 8, 2012 at 9:49
2

I think you could use blacktex on the lines of black formatter for python by Nico Schlömer. From the documentation

blacktex is a little tool, helping with the article editing for LaTeX. It removes all comments from a given file and corrects some common anti-patterns. For example, with

blacktex in.tex out.tex

the input file

Because   of $$a+b=c$$ ({\it Pythogoras}),
% @johnny remember to insert name
and $y=2^ng$ with $n=1,...,10$, we have ${\Gamma \over 2}=8.$

is converted to

Because of
\[
a+b = c
\]
(\textit{Pythogoras}),
and \(y = 2^n g\) with \(n = 1,\dots,10\), we have \(\frac{\Gamma}{2} = 8\).

You can use

blacktex -i in.tex
1

I'd be tempted to look at this task using regular expressions, which you can access using a variety of tools such as perl, python and many more.

latexindent.pl has dedicated replacement switches that can assist with this. If you start with the following file, say myfile.tex

myfile.tex

this one has no number
\begin{equation}
a = b
\nonumber
\end{equation}
this one keeps it number
\begin{equation}
a = b
\end{equation}

and employ the following settings in, say, equation.yaml

replacements:
  -
    substitution: |-
      s/(\\begin\{equation\})(.*?)(\\end\{equation\})/
            my $begin = $1;
            my $body  = $2;
            my $end  =  $3;
            if($body=~m|\\nonumber|s){
                $body  =~ s|\\nonumber(\h*\R)?||sg;
                $begin = "\\[";
                $end   = "\\]";
            }
            $begin.$body.$end;/sxge

We call latexindent.pl using

latexindent.pl -rr myfile.tex -l equation.yaml

produces the following output

this one has no number
\[
a = b
\]
this one keeps it number
\begin{equation}
a = b
\end{equation}

You'll find further examples and demonstrations in the latexindent.pl documentation and in various answers on this site:

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.