4

I have been searching a bit, but couldn't find the proper answer to my problem.

I have multiple tables defined for a report. Let's say some tables are presented in the main content of the report and for further clarity, are also defined in the appendix.

Since they are subject to last minute modification, I didn't want to create twice the same tables, so I inserted them with the \input command.

The problem is that when I call \ref, it is the reference of the table in the appendix that is given (ie: A.3 not 1.5).

How could I manage this?

Table definition:

\begin{table}[htbp]
    \centering
    \begin{tabular}{ccc} \hline
    \multicolumn{3}{c}{value test}\\ \hline
    Model & \multicolumn{2}{c}{3D} \\
    $S$ & 102.60 & 66.90 \\
    $S^{macro}$ & $93.43$ & $63.90$\\
    Relative scatter (\%) & $-8.94$ & $-4.48$\\
    \hline
    \end{tabular}
    \caption{$S_{12}$ analysis}
    \label{tab:S12_analysis}
    \end{table}

In the document for example

\begin{document}[report]

Table \ref{tab:S12_analysis}
\input{my_table.tex}

\appendix
Table \ref{tab:S12_analysis}
 \input{my_table.tex}

\end{document}
1
  • For now, the solution I found is to double compile the document without the chapter of the appendix while commenting the /input in the appendix. So LaTeX generate the good ref. of my tables. Then a third compilation while un-commenting in the appendix.
    – Aubry
    Commented Sep 24, 2013 at 8:14

3 Answers 3

4

replace

 \label{tab:S12_analysis}

on your file by \labelthistable

then use

Table \ref{mytableone}
\newcommand\labelthistable{\label{mytable1}}\\input{my_table.tex}

...

\appendix
Table \ref{mynewtable}
\newcommand\labelthistable{\label{mynewtabl1}}\\input{my_table.tex}
3

You also get a

There were multiply-defined labels
Label `tab:S12_analysis' multiply defined

warning.

This has nothing to do with \input, but with the fact that the same label appears twice. Here's how you can do; this solution also sets the same table number in the appendix and the relative caption will not go in the list of tables.

\begin{filecontents}{\jobname-table}
\begin{table}[htbp]
\centering
\begin{tabular}{crr}
\toprule
  \multicolumn{3}{c}{value test}\\
\midrule
  Model & \multicolumn{2}{c}{3D} \\
  $S$ & 102.60 & 66.90 \\
  $S^\textup{macro}$ & $93.43$ & $63.90$\\
  Relative scatter (\%) & $-8.94$ & $-4.48$\\
\bottomrule
\end{tabular}

\doublecaption{tab:S12_analysis}{$S_{12}$ analysis}

\end{table}
\end{filecontents}

\documentclass{book}
\usepackage{amsmath,booktabs}

\usepackage{xparse}

\makeatletter

\NewDocumentCommand\doublecaption { m o m }
 {%
  \ifappendix
    % If we are in the appendix, renew the \thetable or \thefigure
    % command to use the old number; also suppress the generation
    % of the entry in the list of tables/figures
    \@namedef{the\@captype}{\ref{#1}}%
    \renewcommand{\addcontentsline}[3]{}%
    % Print the caption
    \IfNoValueTF{#2}{\caption{#3}}{\caption[#2]{#3}}%
  \else
    % If we are in the main body do as usual
    \IfNoValueTF{#2}{\caption{#3}}{\caption[#2]{#3}}%
    \label{#1}%
  \fi
 }

\newif\ifappendix
\g@addto@macro\appendix{\appendixtrue}

\makeatother

\begin{document}
\mainmatter
\chapter{Test}

A reference to the table~\ref{tab:S12_analysis}

\input{\jobname-table}

\appendix

\chapter{Supplement}

\input{\jobname-table}

\end{document}

The filecontents is just in order to emulate your setup, which is good, because having the same data twice in the document makes it unmaintainable.

Here is the result in the main body:

enter image description here

And here is what appears in the appendix:

enter image description here

1
  • This is pretty much what I wanted! Many thanks for your time and editing my example!
    – Aubry
    Commented Sep 24, 2013 at 13:33
0

Another possible solution (although not in your case) is that \label has been defined before \caption. This does not work!

If anyone is incurring undefined references (??) with \ref or Table ?? with \autoref and have already cleared your .aux files, I suggest checking if this is the case. If so moving the \label statement should fix your error.

You must log in to answer this question.

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