# 'LaTeX Error: Can be used only in preamble' when using \externaldocument

I'm using 'xr' package for cross referencing across files. I have three files named 'main', 'file1' and 'file2' where all the packages are decalred in the main file. I'm including the files in the main file using the \include command.

The content is in the other two files. Now I've to refer to an equation in file1 from from file2.

So I used \externaldocument{file1} in file2. It resulted in an error.

'LaTeX Error: Can be used only in preamble'

Then I used \externaldocument{file1} in main but it resulted in many warnings saying that
'LaTeX Warning: Label kcl' multiply defined'.

• Don't use the xr-package for this. You don't have an external document if you use \include. \ref will work without it. May 18, 2016 at 7:25

# Clarification

If I understand your question correctly, this is how your files are structured.

## File: main.tex

\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc} % Use UTF8 encoding
\usepackage{fullpage}
\usepackage{lipsum}
\usepackage{hyperref}

\begin{document}

\title{Some MWE}
\author{Samuel}
\maketitle

% Include file1.tex
\input{./file1}

% Include file2.tex
\input{./file2}

\end{document}


## File: file1.tex

\section{Introduction}
\label{sec:intro}
\lipsum[1-2]

\section{Method}
\label{sec:method}
\lipsum[1-2]


## File: file2.tex

\section{Results}
\label{sec:results}
\lipsum[1-2]

\section{Conclusion}
\label{sec:conc}
\lipsum[1-2]
\par As seen in section~\ref{sec:intro}, we know that we can
link back to it from a separate file!


# Solution

If you noticed, I've sneaked the solution in above. In the main file I specified the preamble, the packages to be used and declared the document structure.

In the body of the main document (main.tex), I have used the \input{...} command to take the contents of the other .tex files named file1.tex and file2.tex and inject it at the points specified.

When compiled, the file looks like this: (tidied for clarity)

\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc} % Use UTF8 encoding
\usepackage{fullpage}
\usepackage{lipsum}
\usepackage{hyperref}

\begin{document}

\title{Some MWE}
\author{Samuel}
\maketitle

% Include file1.tex
\section{Introduction}
\label{sec:intro}
\lipsum[1-2]

\section{Method}
\label{sec:method}
\lipsum[1-2]

% Include file2.tex
\section{Results}
\label{sec:results}
\lipsum[1-2]

\section{Conclusion}
\label{sec:conc}
\lipsum[1-2]
\par As seen in section~\ref{sec:intro}, we know that we can
link back to it from a separate file!

\end{document}


From the above, you can see that the cross referencing will work just fine with the standard \ref and \ref* and also their enhanced forms such as when used with the hyperref` package as above.