General ⇒ How to cross-refer to objects in another file?
How to cross-refer to objects in another file?
Hi,
I have put my figures and tables in different files (.tex file) separate from the document file. In each file, I have labeled the figures and tables (with \label). As I use kile, I have added these files to the relevant project. However, when referring to these objects with \ref, the reference is undefined. How can I make them recognised in other files?
I have put my figures and tables in different files (.tex file) separate from the document file. In each file, I have labeled the figures and tables (with \label). As I use kile, I have added these files to the relevant project. However, when referring to these objects with \ref, the reference is undefined. How can I make them recognised in other files?
NEW: TikZ book now 40% off at Amazon.com for a short time.

- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
How to cross-refer to objects in another file?
It's better to demonstrate this behaviour with a minimal working example (MWE). Including the file with the \include command should work because their AUX files are read to build proper references to labels.
Best regards
Thorsten¹
Best regards
Thorsten¹
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
How to cross-refer to objects in another file?
Suppose that I have macros (see below) that are used for all figures and tables. Instead of pasting them in all figure and table files, how can I make one definition to be used by all? I had put them in a file and had included \include in each figure and table files but it's not allowed to nest \include
./Fig.tex:1:\include cannot be nested. \include{header.tex}
./Fig.tex:1:\include cannot be nested. \include{header.tex}
Code: Select all
\newcommand*\mycaption[2]{\caption[#1]{#1 #2}}
\newenvironment{narrow}[2]{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}}%
\item[]}{\end{list}}
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Re: How to cross-refer to objects in another file?
These lines go into the preamble of your document to make them global.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
How to cross-refer to objects in another file?
I realized that \include includes the object whereas in the document, I just want to refer to the figure and print the figure in another file. If I don't \include, the figure is not recognized
./try.tex:6: Reference `fig:try' on page 1 undefined on input line 6.
try.tex:
image.tex
./try.tex:6: Reference `fig:try' on page 1 undefined on input line 6.
try.tex:
Code: Select all
\documentclass[titlepage,12pt,twoside,a4paper]{report}
\usepackage[pdftex]{color,graphicx}
\begin{document}
%\include{image}
My figure is Figure\ref{fig:try}.
\end{document}
Code: Select all
\begin{figure}[h]
\centering
\includegraphics[scale = 2]{try.png}
\caption{This is an image.}
\label{fig:try}
\end{figure}
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
How to cross-refer to objects in another file?
Here we have the best argument for providing a MWE right at the beginning. Only now it becomes clear to me what you want to do. Use the \input command instead.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
How to cross-refer to objects in another file?
\input is also prints the figure which is not desired. I just only print
My figure is Figure 1.
My figure is Figure 1.
Code: Select all
\documentclass[titlepage,12pt,twoside,a4paper]{report}
\usepackage[pdftex]{color,graphicx}
\begin{document}
\input{image}
My figure is Figure\ref{fig:try}.
\end{document}
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
How to cross-refer to objects in another file?
Obviously not so clear as I thought. Try the xr package. The figures need to be part of a complete document because there has an AUX file to be read for getting the references.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
How to cross-refer to objects in another file?
That's do the job.
Now to print figures and tables as separate files, I realized that they should be defined in a \documentclass and all necessary packages and macros should be defined in each file. There is no elegant way to declare or define these packages and macros (sent previously) once and share them between all figures and tables files?
Now to print figures and tables as separate files, I realized that they should be defined in a \documentclass and all necessary packages and macros should be defined in each file. There is no elegant way to declare or define these packages and macros (sent previously) once and share them between all figures and tables files?
Code: Select all
\documentclass[titlepage,12pt,twoside,a4paper]{report}
\usepackage[pdftex]{color,graphicx}
\begin{document}
\begin{figure}[h]
\centering
\includegraphics[scale = 2]{try.png}
\caption{This is an image.}
\label{fig:try}
\end{figure}
\end{document}
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Re: How to cross-refer to objects in another file?
At the moment I'm not aware of any other method. But from my point of view this should be no big deal.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10