# Reference to figures with conditional caption (depending on the version)

I use the versions package for conditional content. However it doesn't work for references with varying captions (i.e., which depend on the version). Here is a MWE

\documentclass[a4paper,11pt]{article}
\usepackage{versions}
\usepackage{lipsum}

\excludeversion{short}
%\includeversion{short}
%\excludeversion{long}
\includeversion{long}

\begin{document}

\begin{short}
\lipsum[1-1]
\end{short}

\begin{long}
\lipsum[1-2]
\end{long}

This is a reference to Fig.~\ref{fig:varyingcaption}, which has a varying caption.

\begin{figure}[htb]
\centering
\framebox(100,50){}
\begin{short}
\caption{SHORT caption}
\end{short}
\begin{long}
\caption{LONG caption}
\end{long}
\label{fig:varyingcaption}
\end{figure}

This is a reference to Fig.~\ref{fig:fixcaption}, which has a fix caption.

\begin{figure}[htb]
\centering
\framebox(100,50){}
\caption{Fix caption}
\label{fig:fixcaption}
\end{figure}

\end{document}

As you can see, the reference for the first figure is ?? while the reference for the the second figure is 2.

I have also tried to use the comment package instead of the versions package and changed the include/exclude commands above as follows

\excludecomment{short}
%\includecomment{short}
%\excludecomment{long}
\includecomment{long}

and have similar problem, but now the ?? has been replaced by a space (i.e., no reference to the first figure is given).

• \begin{long} is a bad idea, since \long has a TeX-internal meaning. However, you have to use the \label inside your long or short environment, not outside, otherwise, it would use the last \@currentlabel from another \refstepcounter call, which could be anything (or just empty) – user31729 Feb 27 '17 at 17:17

To begin with: The package version seems to define environments like the name of \includeversion{foo} etc, i.e. \begin{foo}...\end{foo} will be defined.

This is basically no problem, but the starter command of the environment foo is \foo, so calling the environment long is really bad, since \long is a TeX primitive, it should not be redefined!

Now, the label issue:

If you hide the \caption inside an environment, the \@currentlabel redefined by \refstepcounter does not make it's definition outside of the environment. In order to inform \label about \@currentlabel, you have to use it inside the versions environments.

\documentclass[a4paper,11pt]{article}
\usepackage{versions}
\usepackage{lipsum}

%\excludeversion{shortversion}
\includeversion{shortversion}
\excludeversion{longversion}
%\includeversion{longversion}

\begin{document}

\begin{shortversion}
\lipsum[1-1]
\end{shortversion}

\begin{longversion}
\lipsum[1-2]
\end{longversion}

This is a reference to Fig.~\ref{fig:varyingcaption}, which has a varying caption.

\begin{figure}[htb]
\centering
\framebox(100,50){}
\begin{shortversion}
\caption{SHORT caption}
\label{fig:varyingcaption}
\end{shortversion}
\begin{longversion}
\caption{LONG caption}
\label{fig:varyingcaption}
\end{longversion}

\end{figure}

This is a reference to Fig.~\ref{fig:fixcaption}, which has a fix caption.

\begin{figure}[htb]
\centering
\framebox(100,50){}
\caption{Fix caption}
\label{fig:fixcaption}
\end{figure}

\end{document}