GeneralHow to cross-refer to objects in another file?

LaTeX specific issues not fitting into one of the other forums of this category.
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

How to cross-refer to objects in another file?

Post by carol »

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?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

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?

Post by localghost »

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¹
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

How to cross-refer to objects in another file?

Post by carol »

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}

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}}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: How to cross-refer to objects in another file?

Post by localghost »

These lines go into the preamble of your document to make them global.
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

How to cross-refer to objects in another file?

Post by carol »

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:

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}
image.tex

Code: Select all

\begin{figure}[h]
\centering
 \includegraphics[scale = 2]{try.png}
\caption{This is an image.}
\label{fig:try}
\end{figure}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

How to cross-refer to objects in another file?

Post by localghost »

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.
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

How to cross-refer to objects in another file?

Post by carol »

\input is also prints the figure which is not desired. I just only print
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}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

How to cross-refer to objects in another file?

Post by localghost »

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.
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

How to cross-refer to objects in another file?

Post by carol »

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?

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}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: How to cross-refer to objects in another file?

Post by localghost »

At the moment I'm not aware of any other method. But from my point of view this should be no big deal.
Post Reply