4

Is there any way I can get subfiles to correctly reference ExPex example numbers? Say I had a main file which contained two subfiles. One subfile contains an ExPex example, and the other contains a reference to that example (using the \getref command). The result this produces in the main file is this:

Main file:

This is correct, and I have no problems with this output. However, when I compile fileB.tex as a subfile (the file which references the ExPex example from fileA.tex), I get this:

fileB:

What I want is for fileB to read:

This is a reference to (1).

Is there any way of resolving this? Or is it a problem with subfiles / expex? I've tried using the package xr so that the subfiles point to each other, but this hasn't worked. I need this for a thesis where I will be compiling individual chapters that will point to examples from other chapters in some cases. I've had no problem with the \ref command across subfiles when referencing sections, tables, etc. but still would like a way to cross-reference ExPex examples. Any help would be immensely appreciated, thanks!

MWE with code for main.tex:

\documentclass{article}

\usepackage{expex}
\usepackage{subfiles}

\begin{document}

\subfile{fileA}
\subfile{fileB}

\end{document}

fileA.tex:

\documentclass[./main]{subfiles}

\begin{document}

\section{Original}

\ex<ex1> This is a test.
\xe

\end{document}

and fileB.tex:

\documentclass[./main]{subfiles}

\begin{document}

\section{Reference}

This is a reference to (\getref{ex1}).

\end{document}
10
  • Welcome to TeX.se. ExPex doesn't use LaTeX counters, but I think you can use regular \label and \ref with ExPex examples instead of using the ExPex methods. Would this be a practical solution? If not it may be some work to get the ExPex count registers to work in the way you want.
    – Alan Munn
    Commented Aug 29, 2023 at 3:35
  • Haven't tried it myself, but you may want to have a look at expex-acro
    – Niranjan
    Commented Aug 29, 2023 at 3:36
  • @Niranjan expex-acro doesn't really provide anything specific to this problem.
    – Alan Munn
    Commented Aug 29, 2023 at 3:42
  • @AlanMunn It I guess has a referencing method which does use LaTeX labels
    – Niranjan
    Commented Aug 29, 2023 at 3:47
  • @Niranjan Yes, but using LaTeX labels with ExPex doesn't depend on using it.
    – Alan Munn
    Commented Aug 29, 2023 at 3:52

2 Answers 2

3

Add the following lines to the preamble of your main file:

\gathertags
\input{XXX-tags}% XXX is the name of the main file (without .tex)

Run LaTeX at least once on your main file to collect the labels, before LaTeXing the subfiles. Likewise, whenever some labels change in another subfile, you have to run LaTeX again on the main file in order to write the new information to XXX-tags.tex.

In your example, the file main.tex would look like

\documentclass{article}

\usepackage{expex}
\usepackage{subfiles}
\gathertags      % <<<<<<<<<<<<<<<<<<<<<<<<
\input{main-tags}% <<<<<<<<<<<<<<<<<<<<<<<<
\begin{document}

\subfile{fileA}
\subfile{fileB}

\end{document}

After running pdflatex main you can do pdflatex fileB, and will obtain the reference to (1).

2
  • +1 Totally forgot about \gathertags.
    – Alan Munn
    Commented Sep 1, 2023 at 18:47
  • Wow this is really elegant and simple, thank you so much! Commented Sep 2, 2023 at 3:34
2

In my opinion, \inlcude should perfectly do for this case. If you split document by chapters, with a macro \chapter, you can avoid all this hassle with references if you append \includeonly in preamble. Just beware \include generates an extra page break, similarly to \chapter, so you shouldn't use it with documents split into sections, subsections etc. This would be the only inconvenience.

Consider the code below for a self-contained file that generates additional three files to include:chapeter1.tex, chapter2.tex and chapter3.tex. The document is split into chapters, which are included in the main file via \include. For instance if I wanted to produce only chapter 2, I'd just append \includeonly{chapter2} in my preamble. All reference would still work

\begin{filecontents*}[overwrite]{chapter1.tex}
\chapter{First}
\kant[1][1]

\begin{table}[tbh]
  \caption{Data in chapter 1}\label{tab:ch1}
\end{table}

References: \cref{fig:ch2,fig:ch3,tab:ch3}.
\end{filecontents*}
%
\begin{filecontents*}[overwrite]{chapter2.tex}
\chapter{Second}
\kant[1][2]

\begin{figure}[tbh]
  \caption{Graph in chapter 2}\label{fig:ch2}
\end{figure}

References: \cref{tab:ch1,fig:ch3,tab:ch3}.
\end{filecontents*}
%
\begin{filecontents*}[overwrite]{chapter3.tex}
\chapter{Third}
\kant[1][3]

\begin{figure}[tbh]
  \caption{Graph in chapter 3}\label{fig:ch3}
\end{figure}

\kant[1][4]

\begin{table}[tbh]
  \caption{Data in chapter 1}\label{tab:ch3}
\end{table}

Refereces: \cref{tab:ch1}, \cref{fig:ch2}.
\end{filecontents*}
%%%
\documentclass{report}
\usepackage{kantlipsum}
\usepackage[colorlinks]{hyperref}
\usepackage[noabbrev]{cleveref}

\includeonly{chapter2}


\begin{document}
\include{chapter1}
\include{chapter2}
\include{chapter3}
\end{document}

enter image description here

2
  • Thanks for this! I hadn’t really considered this workflow but it looks like a really neat solution. In this case, though, wouldn’t each chapter compiled via the \includeonly command still have the thesis title page, toc, etc.? Or does that command simply compile the file named in the argument with the preamble of the main file, and nothing else? Commented Aug 29, 2023 at 11:31
  • It does compile everything else meaning the title, table of content, list of figures, list of tables, bibliography etc. \includeonly affects arguments of \include. For instance, if your Appendix was in the main document (not included by \include), it would also be compiled.
    – Celdor
    Commented Aug 29, 2023 at 11:53

You must log in to answer this question.

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