Home > Mobile >  parse references in latex auxiliary file with python
parse references in latex auxiliary file with python

Time:10-16

If you compile a latex file containing labels, the auxiliary file will contain the string associated with every labels.

For example, from the following latex source

\documentclass{article}

\begin{document}
\begin{equation}
  e^{i\pi} = - 1
  \label{myequation}
\end{equation}
\end{document}

after compiling a .aux file is generated, containing:

\relax 
\newlabel{myequation}{{1}{1}}
\gdef \@abspage@last{1}

From this file one can see that the equation referenced with myequation has label "1".

I am looking for a python class that parses the .aux files and generates a dictionary with the correspondence label_name->string.

Is there a python module or parser that does that?

Additional information

The reason why I need this is the following. I have 2 latex source files, let's say file1.tex and file2.tex. file2.tex contains references to equations/tables/etc which are present only in file1.tex. Since the actual string associated to every label in file1.tex is dynamically generated at compile time, my idea would be to

  1. compile file1.tex

  2. extract the correspondence label->string

  3. substitute \ref{somelabel} with the actual string in file2.tex with a parser

  4. compile file2.tex

Step 3 is easily done, for example, using sed. Step 2 is perhaps more cumbersome.

CodePudding user response:

You can simply access the labels from the other document with the xr package (or xr-hyper if you're using hyperref):

\documentclass{article}

\usepackage{xr}

\externaldocument{file1}

\begin{document}

test \ref{myequation}

\end{document}

enter image description here

  • Related