TransWikia.com

Understanding how cross-referencing LaTeX projects with subfiles

TeX - LaTeX Asked by Christopher Creveling on March 5, 2021

I am trying to cross-reference an entire subfile, i.e. not an individual section/subsection, and am unable to get the reference of a different subfile document to show up. All figures/tables/equations/references work perfectly fine.

Essentially, I have a main Dissertation.tex file with multiple subfiles (Chapter1, Chapter2, etc.) and each subfile has multiple subfiles (Intro1, Methods1, etc.).

dissertation/
|-- Dissertation.tex
|-- Dissertation.cls
|-- makefile (Dissertation, release, clean)
|-- Release/
|   |-- Dissertation.pdf
|-- Chapter1/
|   |-- Intro1.tex
|   |-- Methods1.tex
|   |-- Results1.tex
|   |-- Discussion1.tex
|   |-- Conclusion1.tex
|   |-- Figures/
|       | -- Figure1.pdf
|       | -- Figure2.pdf
|       | -- Figure3.pdf
|   |-- bib_files/ 
|       | -- bib1.bib
|   |-- makefile (Chapter1, Intro1, Methods1, release, clean, etc.)
|   |-- Release/
|   |-- | -- Chapter1.pdf
|   |-- | -- Intro1.pdf
|   |-- | -- Methods1.pdf
|-- Chapter2/
|   |-- Intro2.tex
|   |-- Methods2.tex
|   |-- Results2.tex
|   |-- Discussion2.tex
|   |-- Conclusion2.tex
|   |-- bib_files/ 
|       | -- bib2.bib
|   |-- Figures/
|       | -- Figure1.pdf
|       | -- Figure2.pdf
|       | -- Figure3.pdf
|   |-- makefile (Chapter2, Intro2, Methods2, release, clean, etc.)
|   |-- Release/
|   |-- | -- Chapter2.pdf
|   |-- | -- Intro2.pdf
|   |-- | -- Methods2.pdf
|-- AppendixA/
|   |-- AppendixA.tex
|   |-- Code/
|       | -- code1.py
|       | -- code2.m
|       | -- code3.cc
|   |-- Data/
|       | -- Data1.csv
|       | -- Data2.csv
|       | -- Data3.csv

I am using a Linux Makefile to compile the document and have incorporated the command latexmk to help with multiple compile times to get the references to work properly.

# Linux Makefile to create a final pdf of the project

# Variables
FILE = Dissertation

# PDF LaTeX specific
PDFLATEX = "pdflatex -interaction=nonstopmode -synctex=1 --shell-escape"

# You want latexmk to *always* run, because make does not have all the info.
# Also, include non-file targets in .PHONY so they are run regardless of any
# file of the given name existing.
.PHONY: ${FILE}.pdf all clean

# The first rule in a Makefile is the one executed by default ("make"). It
# should always be the "all" rule, so that "make" and "make all" are identical.
all: ${FILE}.pdf

# CUSTOM BUILD RULES

# In case you didn't know, '$@' is a variable holding the name of the target,
# and '$<' is a variable holding the (first) dependency of a rule.
# "raw2tex" and "dat2tex" are just placeholders for whatever custom steps
# you might have.

%.tex: %.raw
    ./raw2tex $< > $@

%.tex: %.dat
    ./dat2tex $< > $@

# MAIN LATEXMK RULE

# -pdf tells latexmk to generate PDF directly (instead of DVI).
# -pdflatex="" tells latexmk to call a specific backend with specific options.
# -use-make tells latexmk to call make for generating missing files.

# -interaction=nonstopmode keeps the pdflatex backend from stopping at a
# missing file reference and interactively asking you for an alternative.

# --shell-escape allows for *minted to run code highlighting

# -f forces latexmk to run until compiling has been complete regardless of
#  cross referencing (i.e. it continues to run until references are in the
#  correct location)

${FILE}.pdf: ${FILE}.tex
    latexmk -pdf -pdflatex=${PDFLATEX} -f -use-make ${FILE}.tex

# Clean up unnecessary files
clean:
    latexmk -C
    # specific to latexmk

clear:  
    rm -rf auto *_minted-* *.log *.aux *.synctex.gz *.out *.toc *.run *.bcf *.lof *.lot *.tdo *.run.xml *.pdf *.bbl *.blg

release:
    rm -rf Release
    mkdir Release
    cp *.pdf Release
    make clear
    make clean

Here is how I have set up Chapter2.tex.

%%% -*-LaTeX-*-
documentclass[../Dissertation]{subfiles}

graphicspath{{Chapter2/Figures/}{Figures/}} % Graphics path for images
addbibresource{bib_files/bibliography.bib} % Chapter 2 references

begin{document}label{Chapter2}
    
    % Needs to be capitalized (University Rules)
    chapter{uppercase{Title}}
    
    hspace{parindent}
    Experimental data measurement.
    
    subfile{./Chapter2/0_Abstract}
    subfile{./Chapter2/1_Introduction}
    subfile{./Chapter2/2_Methods}
    subfile{./Chapter2/3_Results}
    subfile{./Chapter2/4_Discussion}
    subfile{./Chapter2/5_Conclusion}
    subfile{./Chapter2/6_Acknowledgment}
    
    % bibliography
    printbibliography[segment=therefsegment,heading=references]
    
end{document}

I tried adding label{Chapter2} in the Chapter2.tex file and want to reference this in Chapter4.tex by saying … ref{Chapter2}… but this doesn’t seem to work.

%%% -*-LaTeX-*-
documentclass[../Dissertation]{subfiles}

doublespacing
graphicspath{{Chapter4/Figures/}{Figures/}} % Graphics path for images

begin{document}

section{Methods}label{Methods_4}
Lipsum[8] 

subsection{FEA input data}label{InputData_4}
Using the experimental data from ref{Chapter2}, the maximum force was determined using ...

end{document}

Thanks!

One Answer

The way I have understood your question, you want to reference a chapter not the (sub)file itself.

All you have to do for that is to place label{Chapter2} after chapter{uppercase{Title}} rather than begin{document}.

Correct answer by shekura on March 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP