mercredi 3 décembre 2014

Extracting the pagenumber of the first page of a section from the toc file


In a project I am working on, I use subfiles to compile a document, each containing one section. If I compile only the document containing some given section, I want the page number to be correct. One way to solve this would be to set the page number manually before I compile, but I would prefer if this worked automatically. The main file, which compiles the complete document, contains a table of contents, why there is a .toc file which contains this information. Given that this does not seem to be a built in functionality in the subfiles package, and given that I do not want to use another package than subfiles to divide my document into several files, I would therefor want to write a macro which given a section number returns the first page of that section as recorded by the .toc file of the main document.


One way to do this should be to, after reading the contents of this file in some way, use a regular expression to find the pattern {number}{section.X} in the toc file, given a section number X. However, when trying to do this with the l3regex package, I got stuck when trying to understand how to use \regex_extract_once to extract the page number for the a section. The code below uses the regular expression { (\d{1}{2}) .. section.X} to find the page number given that X is the section number, but simply replacing replace with extract does not work.



\documentclass{article}

\usepackage{l3regex}


\ExplSyntaxOn
\tl_new:N \l_demo_tl
\seq_new:N \l_demo_res

\cs_new:Npn \demo #1 {

\tl_set:Nn \l_demo_tl {#1}
\regex_replace_once:nnN { (\d{1,2}) .. section\.1} {..............\1...............} \l_demo_tl
\l_demo_tl
}

\ExplSyntaxOff
\begin{document}

\demo{contentsline {section}{numberline {1}Aritmetik och algebra}{3}{section.1}}

\end{document}


So, is there some easier way to obtain this, or alternatively, how does \regex_extract_once work, i.e. how can the code above be adjusted to print the matched string?





Aucun commentaire:

Enregistrer un commentaire