5

I am trying to implement LaTeX at my company to avoid a lot of manual work and errors 😊

A report contains the same section multiple times – with only several component “tags” varying. The tags could be e.g. Qn_11 in first section, Qn_12 in second section and so on. The body text does not change, so when the document is copied in Word, the tags must be changed manually.

In LaTeX I was happy to discover that the same file can be inputted multiple times. The corresponding “tag” can be redefined with \renewcommand{}{}. However, a problem occurs with cross references. Unsurprisingly the multiply-defined labels warning pops up.

I only need to refer “inside” each section, i.e. the specific inputted file. Is there a way of “resetting” the labels or something similar before each input, so they can be used over and over within the single inputted file? I consider turning the sections into chapters. The objects (figures, tables, equations) will then be numbered 1.x, the first time the file is inputted, 2.x the next time and so on. If something can be worked out 😊 Thank you in advance!

A sniplet of the main file:

\documentclass{memoir}
\usepackage{graphicx}
\pagestyle{empty}

\begin{document}
\setcounter{chapter}{1}

\newcommand{\MyTag}{Qn-11}
\input{fileex}

\renewcommand{\MyTag}{Qn-12}
\input{fileex}

\end{document}

A sniplet of the inputted file:

\section{Header}

The component setting is \MyTag. See figure \ref{fig:setting}.

\begin{figure}[htbp]
\includegraphics[width=3cm]{example-image-a}
\caption{Caption}
\label{fig:setting}
\end{figure}

And the result (with the cross reference issue highlighted):

Error due to multiply-defined labels

| improve this question | | | | |
  • 1
    Welcome! How about using \ref{\MyTag fig:setting} and \label{\MyTag fig:setting} in the fileex.tex file? – Schrödinger's cat Nov 23 '19 at 19:22
  • Of course, I am tempted to say :) Very clever and helpful implementing the tag in the label! Is it best you leave an answer with that or should I accept the prepend-answer below? – aliaz Nov 23 '19 at 19:33
  • 1
    The answer below uses the same trick but it does not require you to change anything in fileex, which is why I wrote it that way. You could also define new commands \Ref and \Label, \newcommand{\Label}[1]{\label{\MyTag#1}} \newcommand{\Ref}[1]{\ref{\MyTag#1}}, and replace all \ref and \label by \Ref and \Label in fileex.tex. What is "best" depends a bit on what you want to do on the long run. – Schrödinger's cat Nov 23 '19 at 19:38
  • @Schrödinger'scat Your first suggestion will break fancyref, whereas \ref{fig:setting\MyTag}, for instance, won't. (Obviously, not everyone uses fancyref, but it is easy to avoid the potential breakage.) – cfr Nov 23 '19 at 21:58
  • @cfr Thanks for the heads up! – Schrödinger's cat Nov 23 '19 at 22:01
5

Welcome! You can just append (big thanks to Phelype Oleinik and cfr for pointing out that appending works with hyperref!) \MyTag to the labels

\documentclass{memoir}
\usepackage{graphicx}
% \usepackage{hyperref}
% \usepackage{cleveref}
\newcommand{\MyLabel}[1]{\label{\MyTag#1}}
\newcommand{\MyRef}[1]{\ref{\MyTag#1}}

\pagestyle{empty}

\begin{document}
\setcounter{chapter}{1}

\newcommand{\MyTag}{Qn-11}
\input{fileex}

\renewcommand{\MyTag}{Qn-12}
\input{fileex}

\end{document}

This requires you to replace \label and \ref to \MyLabel and \MyRef, respectively, in fileex.tex:

\section{Header}

The component setting is \MyTag. See figure \MyRef{fig:setting}.

\begin{figure}[htbp]
\includegraphics[width=3cm]{example-image-a}
\caption{Caption}
\MyLabel{fig:setting}
\end{figure} 

enter image description here

I have tested that this works with hyperref, cleveref and both of them combined. I could not make it work with fancyref, but there seems to be some conflict between memoir and fancyref. A long discussion of these mutual (in)compatibilities can be found here.

In the current settings you could also prepend the labels by \MyTag.

\documentclass{memoir}
\usepackage{graphicx}
\pagestyle{empty}
\let\oldlabel\label
\let\oldref\ref
\renewcommand{\label}[1]{\oldlabel{\MyTag#1}}
\renewcommand{\ref}[1]{\oldref{\MyTag#1}}
\begin{document}
\setcounter{chapter}{1}

\newcommand{\MyTag}{Qn-11}
\input{fileex}

\renewcommand{\MyTag}{Qn-12}
\input{fileex}

\end{document}

As mentioned above, this solution may not work if you use more fancy referencing methods.

| improve this answer | | | | |
  • 1
    Probably not a very good idea to redefine \label: too many packages rely on its definition. It's better to manually add \MyTag or to go with \mylabel/\myref commands which add \MyTag. – Phelype Oleinik Nov 23 '19 at 19:50
  • @PhelypeOleinik Yes, these options are the contents of this comment and this comment. Thanks for the heads up,I added it explicitly to the answer. – Schrödinger's cat Nov 23 '19 at 19:53
  • Oh, I didn't see the comments, sorry. Yes, I'd go for the custom \ref/\label commands because they save the manual labour of adding \MyTag every single time (which you just added to your answer, thanks :-). As an example of the issue with redefining \ref and \label, try to load hyperref (nothing breaks, but iit doesn't work either). P.S.: I'd move the second answer to the top ;-) – Phelype Oleinik Nov 23 '19 at 20:05
  • @PhelypeOleinik Thanks again! I hope to have addressed some of these issues in the edit. – Schrödinger's cat Nov 23 '19 at 22:40
  • Looks great now. Thanks! :-) – Phelype Oleinik Nov 23 '19 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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