# Referring to label and value of counter at that point

I'm putting together a catalogue of artworks. These are given a running Roman number, and divided into groups given letters (so A1, A2, B3, etc.). At the moment I am manually looking to see what group an item appears in and manually referring to the section and then the individual item: so for instance, calling "A" and then adding "1".

However this is not ideal, as doing it manually it is too easy to accidentally give the wrong group for a painting (i.e., in the example below, to refer to A3, when painting 3 is in group B).

I wondered therefore if there is a way to automatically get the value of the counter at the point where the label referring to an item appears? I.e., by referring to item 3, also to give the last value of the counter \catsecnum.

Any help and criticism of my method gratefully received!

\documentclass[a4paper]{article}

\begin{document}

\newcounter{catsecnum}
\renewcommand{\thecatsecnum}{\Alph{catsecnum}}
\newcounter{catnum}

\newenvironment{catalogue}[1][]{\noindent\refstepcounter{catnum}{\bfseries\Alph{catsecnum}\arabic{catnum} {\scshape{#1}}\smallskip\noindent\ignorespaces}}{\bigskip}

\refstepcounter{catsecnum}\label{typeA}
\subsection*{\thecatsecnum : Type A paintings}

\begin{catalogue}
Data regarding painting 1 of type A here.\label{cat:1}
\end{catalogue}

\begin{catalogue}
Data regarding painting 2 of type A here.\label{cat:2}
\end{catalogue}

\refstepcounter{catsecnum}\label{typeB}
\subsection*{\thecatsecnum : Type B paintings}

\begin{catalogue}
Data regarding painting 3 of type B here.\label{cat:3}
\end{catalogue}

\begin{catalogue}
Data regarding painting 4 of type B here.\label{cat:4}
\end{catalogue}

\section{Discussion}

Here I may refer to the fourth painting, which is of type B, by the reference  \ref{typeB}\ref{cat:3}. But I may make mistakes and refer to \ref{typeA}\ref{cat3}.

\end{document}

• Just so I understand the intended use, why are you using the * form of the section to suppress numbering, then numbering by hand using \refstepcounter, which looks odd/complicated? – David Carlisle Feb 11 '15 at 19:48
• This comes at the end of a large document and I did that so the sections could be distinct by having a letter instead of longer arabic number. However, if you can show me a more sensible way to do it, I would be very grateful. – alice19 Feb 11 '15 at 19:56

It is possible to accumulate a bunch of detail regarding a label in what zref calls a property list. Below I've implemented a very basic form of that, where every \labelpainting{<label>} marks the painting type, painting number and page (within the document).

\documentclass{article}
\usepackage{zref}

\makeatletter
\zref@newprop{type}[?]{\thesection}
\zref@newprop{painting}[?]{\thesubsection}

\renewcommand{\thesection}{\Alph{section}}
\newcommand{\newtype}[1][\relax]{%
\refstepcounter{section}%
\zref@setcurrent{painting}{}% No paintings
\ifx\relax#1\relax
\section*{Type~\thesection~Paintings}
\else
\section*{Type~\thesection~Paintings: #1}
\fi}%
\renewcommand{\thesubsection}{\arabic{subsection}}
\newcommand{\newpainting}[1][\relax]{%
\refstepcounter{subsection}%
\zref@setcurrent{default}{\thesection\thesubsection}%
\zref@setcurrent{painting}{\thesubsection}%
\ifx\relax#1\relax
\subsection*{Painting~\thesubsection}
\else
\subsection*{Painting~\thesubsection: #1}
\fi}

\newcommand{\labelpainting}[1]{%
\zref@labelbyprops{#1}{type,painting,page,default}%
}
\newcommand{\refpainting}[2][default]{%
\zref@refused{#2}%
\zref@extract{#2}{#1}%
}
\makeatother

\begin{document}

\newtype[Flemish] \label{type:A}

\newpainting
Data regarding painting~1 of type~A here.\labelpainting{painting:A}

\newpainting[Berlin]
Data regarding painting~2 of type~A here.\labelpainting{painting:B}

\newtype \label{type:B}

\newpainting[South Africa]
Data regarding painting~1 of type~B here.\labelpainting{painting:C}

\newpainting
Data regarding painting~2 of type~B here.\labelpainting{painting:D}

\section*{Discussion}

Here I may refer to the~\refpainting[painting]{painting:D}, which is
of type~\refpainting[type]{painting:D}, by the reference \refpainting{painting:D}.

\end{document}


Both \newtype[<opt arg>] and \newpainting[<opt arg>] take an optional argument. If it is used, it appends : <opt arg> to the respective title.

I've used \sections to denote a type, and \subsections to denote a painting. However, this can be extended/adapted to whatever is required.

The advantage of this approach is that you can label a host of attributes (or properties) associated with a single painting, and extract them later using different techniques. The regular \label-\ref system allows for only two properties (label and page).

• Thank you so much. Could you show me how to add an argument to define the type of group, (i.e. say not just "type a" but specify "type a: Flemish") like there is for specifying painting location? – alice19 Feb 11 '15 at 23:52
• @marcellinus12: I've added that functionality to \newtype. It already existed for \newpainting. – Werner Feb 12 '15 at 0:03

I would arrange the counting/references like this, if the formatting isn't as needed the heading layout can be customised in the usual ways.

\documentclass[a4paper]{article}

\makeatletter
\newcounter{type}
\newcommand\type{\@startsection{type}{2}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
\renewcommand\thetype{\Alph{type}}
\newcounter{catnum}
\newcommand\catalogue[1][]{\@startsection{catnum}{3}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
{\normalfont\normalsize\bfseries}[kk]{#1}}
\renewcommand\thecatnum{\Alph{type}\arabic{catnum}}

% toc lines  define these to be \newcommand*\l@type[1]{} if not needed
\newcommand*\l@type{\@dottedtocline{2}{1.5em}{2.3em}}
\newcommand*\l@catnum{\@dottedtocline{3}{3.8em}{3.2em}}
% add marks for page heads (or not here)
\def\catnummark#1{}
\def\typemark#1{}
\makeatother

\begin{document}

\tracingmacros3
\type\label{typea}

\begin{catalogue}[London]
Data regarding painting 1 of type A here.\label{cat:1}
\end{catalogue}

\begin{catalogue}[North Pole]
Data regarding painting 2 of type A here.\label{cat:2}
\end{catalogue}

\type\label{typeb}

\begin{catalogue}[The Louvre]
Data regarding painting 3 of type B here.\label{cat:3}
\end{catalogue}

\begin{catalogue}[Somewhere]
Data regarding painting 4 of type B here.\label{cat:4}
\end{catalogue}

\section*{Discussion}

Here I may refer to the fourth painting, which is of type B, by the reference
\ref{cat:4}.

\end{document}

• Thank you; however, the problem with this is that it will, I think, make problems with section names later on (as in fact happens with the discussion section which gets called Type C). It was to avoid this confusion of the catalogue groups with sections in the work that I used the counter \catsecnum – alice19 Feb 11 '15 at 20:03
• @marcellinus12 reposted with commands separated from \subsection – David Carlisle Feb 11 '15 at 20:33
• Thank you, this is great. I just have two questions: is it possible at all to append text to the line giving the letter and number? The environment I used before was so I could give the location of the painting there, thus "A1: Berlin." Secondly, this is brilliant but I am afraid it will mess up all the other references in the rest of my document? Is there a way to somehow have these catalogue references separate from other cross references so they won't interfere? (I.e. if i subsequently try to refer to an image of the painting, it won't referencce as expected). – alice19 Feb 11 '15 at 21:22
• Sorry this is complex, I apologise if i'm being unclear – alice19 Feb 11 '15 at 21:38
• @marcellinus12 yes I can put an argument back to hold Berlin or whatever. I don't understand your concern about the cross referencing, I changed nothing there that is just standard latex \label behaviour so I don't see how it can affect anything other than this counter? – David Carlisle Feb 11 '15 at 21:43