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.



\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


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


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?

  • Why not simply use macros? Do you want to extract it while reading the .toc file? Can you be more specific about how you want to use this?
    – egreg
    Dec 3, 2014 at 9:56
  • @egreg I am using the subfiles package to compile a large document with several sections, where each section is located in one subfile. If I compile one of the sections (subfiles) separately I want the page numbers to be correct, and this seemed to be one way to to it, i.e. to extract the information from the toc file for the main document. If there is a simpler way to do that(?), without replacing the subfiles package with something else, I would not mind doing that instead..
    – malin
    Dec 3, 2014 at 10:04
  • An example of your setup is needed. But the data are structured, so you just need to examine the third argument to \contentsline, which can be added to the macro itself.
    – egreg
    Dec 3, 2014 at 10:07
  • @egreg How do I do that? (I also edited the question to (hopefully) better explain the setting of the problem)
    – malin
    Dec 3, 2014 at 10:42
  • Why not adding a small example of code?
    – egreg
    Dec 3, 2014 at 10:44

1 Answer 1


Here's my approach to the problem, which has nothing to do with l3regex. I know it doesn't answer the question as you phrased it, but it solves the problem.

File mainfile.tex






\textheight=5cm % just to show the page number in a small picture





File sec1.tex




\section{This is the first section}




After pdflatex mainfile and pdflatex sec1 here's sec1.pdf

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .