Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a few sections of content I want repeated in multiple chapters. When I include it twice it looks fine in the document but references within that section always go to the section that is included last. This makes sense because the labels are being redefined. Is there anyway to change this behavior or perhaps a better way to add a section to multiple chapters. For example, allowing only references to come from the chapter the section resides in.

share|improve this question
    
So this "repeated section" contains both \labels and \refs? And using \label{<lab>} more than once with the same <lab> is a problem (of course). For this repeated section you could turn off the meaning of \label while still keeping \ref. However, this doesn't make sense in terms of your last comment: "allowing only references to come from the chapter the section resides in." References will come from the first place the section was included. Please clarify. –  Werner Feb 4 '13 at 18:47
add comment

1 Answer 1

up vote 7 down vote accepted

Define special commands for labels and references to the elements of the common section.

Main file

\documentclass{book}

\usepackage{hyperref}

\newcommand{\mlabel}[1]{\label{\thechapter@@#1}}
\DeclareRobustCommand{\mref}[1]{\ref{\thechapter@@#1}}

\begin{document}
\mainmatter
\chapter{First}
\input{common}

\section{A section}

This is a reference to the common section: \mref{commonsection}

\chapter{Second}
\input{common}

\section{A section}

This is a reference to the common section: \mref{commonsection}

\end{document}

File to be included multiple times common.tex

\section{Common section}\mlabel{commonsection}

This is the text that goes in all chapters.

First chapter

enter image description here

Second chapter

enter image description here

share|improve this answer
    
Very clever. Didn't work for me at first but doing \newcommand{\mlabel}[1]{\label{\thechapter_#1}} \DeclareRobustCommand{\mref}[1]{\ref{\thechapter_#1}} did the trick. Didn't like something about the @@. Thanks! –  zlalanne Feb 4 '13 at 19:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.