The question Special case for last element with \foreach manually changed the references, and added the ability to have a prefix on references. Can I do the same thing using \cleveref
?
(Note: This is a \cleveref
version of the question Automatically add prefix to references from another document?)
Approach 1:
Have a \crefex
, or renaming of \cref
, which allows for the addition of a prefix. For example
documentclass{article}
\usepackage{cleveref, xr}
\crefname{equation}{}{}
\crefname{figure}{figure}{Figure}
\newcommand{\crefex}[2][] { %
\cref{#2} %But modify to add #1 as a prefix...
}
\begin{document}
\section{sec1}\label{sec:sec1}
\begin{equation} 1 = 1 \label{eq:eq1} \end{equation}
\begin{figure}\caption{fig1}\label{fig:fig1}\end{figure}
\crefex[TA.]{sec:sec1}, \crefex[TA.]{eq:eq1}, \crefex[TA.]{fig:fig1}
\end{document}
which should output as the references if \crefex
was completed.
section TA.1, (TA.1), figure TA.1
Approach 2: Alternatively, it could autodetect if there is an external reference, and add in some sort of a prefix defined as a variable?
Create a file called external_document.tex
with the following text and compile it a few times.
\documentclass{article}
\usepackage{cleveref}
\crefname{equation}{}{}
\crefname{figure}{figure}{Figure}
\begin{document}
\section{sec1}\label{sec:sec1}
\begin{equation} 1 = 1 \label{eq:eq1} \end{equation}
\begin{figure}\caption{fig1}\label{fig:fig1}\end{figure}
\cref{sec:sec1}, \cref{eq:eq1}, \cref{fig:fig1}
\end{document}
Then create another file with:
\documentclass{article}
\usepackage{cleveref, xr}
\crefname{equation}{}{}
\crefname{figure}{figure}{Figure}
\externaldocument[EX.]{external_document}
\begin{document}
\section{sec1}\label{sec:sec1}
\begin{equation} 1 = 1 \label{eq:eq1} \end{equation}
\begin{figure}\caption{fig1}\label{fig:fig1}\end{figure}
\cref{sec:sec1}, \cref{eq:eq1}, \cref{fig:fig1}
\subsection{External References}
\cref{EX.sec:sec1}, \cref{EX.eq:eq1}, \cref{EX.fig:fig1}
\end{document}
This will correctly spit out both the internal and external references. How might we add in autodetection of the external references which adds the prefix of the \externaldocument
as described above?