1

I've separated my large tables into other individual files for organization and ease of editing, but I can't seem to reference any of them. Can someone clue me into what is going on?

main.tex

\documentclass{report}

\begin{document}

    \input{a.tex}

    Table~\ref{tab:test}

\end{document}

a.tex

\begin{table}[!htb]
    \centering
    \caption{ Test }
    \label{test}
\end{table}

My understanding is that if the label isn't duplicated I should be able to reference this. Without it turning into ??

enter image description here

** Update **

Even the xr package doesn't seem to help as other answers have suggested.

\documentclass{report}
\usepackage{xr}
\externaldocument[a-]{a}

\begin{document}


    \input{a.tex}

    Table\ref{a-test}

\end{document}
JME
  • 131
  • Move \label{test} after \caption{ Test }. –  Nov 04 '18 at 04:01
  • No dice, why would order matter? – JME Nov 04 '18 at 04:05
  • 1
  • Okay, I'll do that from now on but it doesn't answer my question. – JME Nov 04 '18 at 04:10
  • 1
    Did you compile twice? –  Nov 04 '18 at 04:11
  • Not sure what you mean. I've compiled using TeXstudio and also tried deleting the AUX file with no success. – JME Nov 04 '18 at 04:14
  • 1
    Well, the process that converts a tex file into a pdf is called compiling. You set \label{test} but do \ref{tab:test}. Use \label{tab:test} instead. –  Nov 04 '18 at 04:20
  • Yes I have compiled multiple times. Does the MWE work for you? I went to the trouble of creating it. This is only a problem with referencing tables, figures, etc from a \input. Keeping the table within the same file works but is undesirable. – JME Nov 04 '18 at 04:33

1 Answers1

5

This is not an answer, just to say: yes, it works. Your original file had two issues:

  1. \label was before \caption, but this does not work.
  2. You label the table \label{test} but refer to it with \ref{tab:test}. However, you need to use the same label as reference.

After fixing the issues, it works.

\documentclass{report}
\usepackage{filecontents}
\begin{filecontents*}{a.tex}
\begin{table}[!htb]
    \centering
    \caption{ Test }
    \label{tab:test}
\end{table}
\end{filecontents*}
\begin{document}

    \input{a.tex}

    Table~\ref{tab:test}

\end{document}

enter image description here