3

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?

malin
  • 3,727
  • 4
  • 23
  • 35
  • 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 03 '14 at 09: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 03 '14 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 03 '14 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 03 '14 at 10:42
  • Why not adding a small example of code? – egreg Dec 03 '14 at 10:44
  • @egreg Because both the mainfile and the section files are more or less empty (contains nothing more than the basic commands that is needed for the subfile package to work) and my main problem at the moment is how to use one specific command from a single package? – malin Dec 03 '14 at 10:50
  • The example can suggest a different way for solving your problem. – egreg Dec 03 '14 at 11:44
  • \regex_extract_once:nnN has different arguments than \regex_replace_once:nnN so it's not just a matter of replacing one function into another. That said, even using the right syntax fails here because the extracted part does not have balanced braces so it cannot be extracted (on top of this, there is currently a bug, see https://github.com/latex3/latex3/issues/377 ). Besides @egreg's solution a work-around is to turn the input to a string before applying \regex_extract_once:nnN. – Bruno Le Floch Jul 12 '17 at 16:27

1 Answers1

2

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

\documentclass{article}
\usepackage{subfiles}

\makeatletter
\long\def\@longempty{}
\newcommand{\ifonlysubfile}{%
  \ifx\document\@longempty
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\usepackage{refcount}
\usepackage{currfile}

\usepackage{xr-hyper}
\makeatletter
\@ifundefined{preamble@file}{}{\externaldocument{\preamble@file}}
\makeatother

\usepackage{hyperref}

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

\begin{document}
\tableofcontents

\clearpage

\subfile{sec1}

\end{document}

File sec1.tex

\documentclass[mainfile]{subfiles}

\ifonlysubfile
  {}
  {\setcounter{page}{\getpagerefnumber{\jobname.tex-page}}}


\begin{document}

\section{This is the first section}
\ifonlysubfile
  {\label{\currfilename-page}}
  {}

Text

\end{document}

Result

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

enter image description here

egreg
  • 1,121,712