[Tex/LaTex] TeXStudio – Cross-reference in case of multiple files

cross-referencingmultiple filestexstudio

I have a master document and for each chapter a separate file like:

main.tex

% preamble etc.

\input{chapter1}
\input{chapter2}
\input{chapter3}
% and so on

chapter1.tex

% Some figure with a label, tex code not shown, just the label
\label{fig:chicken}
% Some figure with a label, tex code not shown, just the label
\label{fig:monkey}

This two-legs animal is a chicken as shown in \cref{fig:chicken} % OK! Works within the same chapter! fig. 1

chapter2.tex

Here I like to refer to \ref{fig:monkey} % results in ??

Referring to a label in another chapter results in a double question mark (I did compile multiple times). LyX has the option to set a master document for each "included" tex-file. Document > Settings > Select default master document.

How can I do this in TeXstudio? I've already set main.tex as the master document but can't find a way to refer from its childs back to the master document like in LyX.

Best Answer

It's important to add the \label{} after or inside \caption{}

main.tex

\documentclass{scrartcl}
\usepackage{cleveref}
\usepackage[demo]{graphicx}
\usepackage[justification=RaggedRight, singlelinecheck=false]{caption} 

\begin{document}

 \input{chapter1}
 \input{chapter2}

\end{document}

chapter1.tex

\chapter{Chapter 1}

\begin{figure}[h!]
 \caption{ Test}
  \label{fig:chicken}
  \includegraphics{chicken}
 \end{figure}

chapter2.tex

\chapter{Chapter 2}

Here I like to refer to \cref{fig:chicken}

Output

Here I like to refer to fig. 1.1
Related Question