1

I have a project consisting of Parts, each with multiple Chapters. The Parts are written in main.tex, with each Chapter included as a separate \subfile of main.tex.

Is there a way to automatically include part and chapter numbering when cross-referencing a section (or lower level)?

Example main.tex

\documentclass{book}
\usepackage{xr-hyper}
\usepackage[colorlinks=true, allcolors=blue]{hyperref} 

\begin{document}

% Part 1
\part{}
\label{part: 1}

% Include chapter 1
\subfile{Pt1/Ch1}

\end{document}

Example chapter subfile

\documentclass[../main.tex]{subfiles}

\begin{document}

\Chapter{My first chapter}

\section{My first section}

\subsection{My first subsection}
\label{SS: My first subsection}

\section{My second section}

In \ref{SS: My first subsection} we discussed...

\end{document}

where I'm looking for the \ref command to generate a cross-reference of the form:

In Pt 1, Ch 1, 1.1 we discussed...

Thank you for your help.

1

1 Answer 1

1

Does this achieve what you want?

You can define the label format for section and subsection using command \labelformat{<target heading>}{<format>}.

Such as:

main.tex

\documentclass{book}
\usepackage{xr-hyper}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}
\usepackage{subfiles}
\labelformat{section}{Pt~\arabic{part}, Ch~\thechapter, \thesection}
\labelformat{subsection}{Pt~\arabic{part}, Ch~\thechapter, \thesubsection}

\begin{document}

% Part 1
\part{}
\label{part: 1}

% Include chapter 1
\subfile{Pt1/Ch1}

\end{document}

chapter subfile

\documentclass[../main.tex]{subfiles}

\begin{document}

\chapter{My first chapter}

\section{My first section}
\label{S: My first section}
\subsection{My first subsection}
\label{SS: My first subsection}

\section{My second section}

In \ref{SS: My first subsection} we discussed... In \ref{S: My first section} we discussed...

\end{document}

enter image description here

3
  • This is excellent, thank you. At the risk of digressing - is it possible to also include the title of a section within \labelformat , to generate something like "In Pt1, Ch1, My first section 1.1 we discussed..." ?
    – Bells12
    Commented Jan 4 at 11:57
  • The above query solved in this question.
    – Bells12
    Commented Jan 5 at 14:55
  • @Bells12 Sorry for late response. I am glad the problem has been solved.
    – Tom
    Commented Jan 6 at 20:11

You must log in to answer this question.

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