3

In my thesis I have a few listings and I also want to provide them online because copying the listings from the PDF output is a bit cumbersome (scattered around multiple pages, line numbering is also copied,...). In the PDF it looks likeenter image description here

which I get from

\lstinputlisting[label={lst:ex1},caption={R code for Example~\ref{ex:intro}},frame=single]{../R/Example1/PCAIntro.r}

Now I want to write a PHP script which provides the file PCAIntro.r online and I want to refer to "Example 2.1.0.1" there. So my question is:

How do I get the result 2.1.0.1 of \ref{ex:intro} to a separate file for further processing?

phinz
  • 289
  • 1
    have you tried looking in the .aux file...? – cmhughes Dec 09 '16 at 11:00
  • @cmhughes I have always ignored the many files like aux, bbl, blg, idx, ilg, lof, log, log, out. I have to learn about .aux-files first. It looks promising. Thanks. But is there also a tex command to write a specific \ref to some file? – phinz Dec 09 '16 at 11:14
  • looks like you got a solution, nice work! :) – cmhughes Dec 09 '16 at 12:34

1 Answers1

2

You have to parse the .aux file. The following PHP code solves my problem using regular expressions:

<?php
$contents=file_get_contents("Filename.aux", "r");
$label="ex:intro";
$pattern="/(?:newlabel{".$label."}{{)([.[:alnum:]]*?)}/";
preg_match($pattern,$contents,$match);
echo $match[1];//contains "2.1.0.1"
?>

Simply adjust "Filename.aux" and "$label" appropriately.

phinz
  • 289