Text FormattingReference to custom Counters

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
achim
Posts: 49
Joined: Wed Aug 05, 2009 2:29 pm

Reference to custom Counters

Post by achim »

Hi

I've created a \newcommand which is similar to the \section and \subsection as it contains a counter and a 'sub-counter' (e.g. 2.4).

The only thing that I couldn't figure out yet is how to properly refer to the counters. I read that if one uses \refsetcounter, the counter number should be accessible by the \ref command, but this somehow does not seem to work very well, as illustrated in the example below...

Code: Select all

\documentclass{article}
\setlength{\parindent}{0mm}

\newcounter{thephase} \setcounter{thephase}{0}
\newcounter{thesubphase}[thephase] \setcounter{thesubphase}{0}
\newcommand{\phase}[1]{\noindent%
	\refstepcounter{thephase}\textbf{Phase \arabic{thephase}} --- \texttt{#1}\\%
	}
\newcommand{\subphase}[1]{\noindent%
	\dots\refstepcounter{thesubphase}\textbf{Phase \arabic{thephase}.\arabic{thesubphase}} --- \texttt{#1}\\%
	}

\begin{document}

\phase{Bla bla bla}

\phase{A Phase}
This is just some random text\dots\\

\subphase{A Sub-Phase (with label)\label{pha: mysubphase}}
And some more random text. But this time, I'd like to refer to this phase by setting a label.\\

\subphase{A Sub-Phase}

\phase{The Last Phase (with label)\label{pha: last phase}}

\vspace{1cm}
In this paragraph, I'd like to refer to the label I've set in the first sub-phase and the last phase;\\
Phase \ref{pha: mysubphase} and \ref{pha: last phase} contains\dots\\
\textbf{However, it should print:}\\
Phase 2.1 and 3 contains\dots
\end{document}
fig.png
fig.png (47.77 KiB) Viewed 21519 times
What am I doing wrong here?

Cheers
Last edited by achim on Wed Nov 17, 2010 6:32 am, edited 1 time in total.
OS: Kubuntu
Distribution: TexLive
Editor: Kile

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Reference to custom Counters

Post by frabjous »

It's a pretty bad idea to name counters beginning with "the". This is because whenever you define a counter, LaTeX also defines a corresponding command beginning with "the". So if you define a counter, "phase", you get a command "\thephase", which prints the value of the counter (at a certain spot). If you name the counter "thephase", the corresponding command is "\thethephase" (!), which is bound to be confusing.

Actually the solution to your problem is to redefine the command "\thethesubphase" (or just "\thesubphase" if you sensibly rename the counter to just "subphase") so that it includes the corresponding "\thephase" part.

In other words, try this:

Code: Select all


\documentclass{article}
\setlength{\parindent}{0mm}

\newcounter{phase} 
\setcounter{phase}{0}
\newcounter{subphase}[phase] 
\setcounter{subphase}{0}
\renewcommand{\thesubphase}{\thephase.\arabic{subphase}}
\newcommand{\phase}[1]{\noindent%
   \refstepcounter{phase}\textbf{Phase \thephase} --- \texttt{#1}\\%
   }
\newcommand{\subphase}[1]{\noindent%
   \dots\refstepcounter{subphase}\textbf{Phase \thesubphase} --- \texttt{#1}\\%
   }

\begin{document}

\phase{Bla bla bla}

\phase{A Phase}
This is just some random text\dots\\

\subphase{A Sub-Phase (with label)\label{pha: mysubphase}}
And some more random text. But this time, I'd like to refer to this phase by setting a label.\\

\subphase{A Sub-Phase}

\phase{The Last Phase (with label)\label{pha: last phase}}

\vspace{1cm}
In this paragraph, I'd like to refer to the label I've set in the first sub-phase and the last phase;\\
Phase \ref{pha: mysubphase} and \ref{pha: last phase} contains\dots\\
\textbf{It should print and it does!}\\
Phase 2.1 and 3 contains\dots
\end{document}
You might also want to make use of some of the commands from the chngcntr package for even more control.
achim
Posts: 49
Joined: Wed Aug 05, 2009 2:29 pm

Reference to custom Counters

Post by achim »

Thanks very much!
This solved the problem. Here's the working solution:

Code: Select all

\documentclass{article}
\setlength{\parindent}{0mm}

\newcounter{phase} \setcounter{phase}{0}
\newcounter{subphase}[phase] \setcounter{subphase}{0}

\renewcommand{\thephase}{\Roman{phase}}
\renewcommand{\thesubphase}{\Roman{phase}$^{\rm\Roman{subphase}}$}

\newenvironment{phase}[1]%
{
\refstepcounter{phase}
\vspace{1ex}
\flushleft\makebox[13ex][l]{\textbf{Phase} \thephase} \texttt{#1}\\
}%
{}
\newenvironment{subphase}[1]%
{
\refstepcounter{subphase}
\vspace{1ex}
\flushleft\makebox[13ex][l]{\textbf{Phase} \thesubphase} \texttt{#1}\\
}%
{}

\begin{document}

\phase{Bla bla bla}

\phase{A Phase}
This is just some random text\dots

\subphase{A Sub-Phase (with label)}\label{pha: a}
\subphase{A Sub-Phase}
\subphase{A Sub-Phase}
\subphase{A Sub-Phase (with label)}\label{pha: b}
\phase{A Phase}
\subphase{A Sub-Phase}
\subphase{A Sub-Phase}
\phase{The Last Phase (with label)}\label{pha: c}

\vspace{1cm}
First Reference: \ref{pha: a}\\
Second Reference: \ref{pha: b}\\
Third Reference: \ref{pha: c}

\end{document}
fig.png
fig.png (36.8 KiB) Viewed 21513 times
Cheers
Achim
OS: Kubuntu
Distribution: TexLive
Editor: Kile
drosa
Posts: 3
Joined: Sun Jan 01, 2012 10:31 pm

Reference to custom Counters

Post by drosa »

The code is great but it does not work when the counter is place within a figure environment. Below, the label miao is unresolved.

Code: Select all

\ref{miao}
\ref{ciao}
\phase\label{ciao}

\begin{figure*}[h]
\begin{scriptsize}
\begin{tabular}{lll}
\phase\label{miao} &
.
.
. (etc)
Does anybody know why?
Last edited by Stefan Kottwitz on Sun Jan 01, 2012 10:54 pm, edited 1 time in total.
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Reference to custom Counters

Post by sommerfee »

drosa wrote:Does anybody know why?
Are you using the caption (or subfig) package? There was a bug regarding \label in floating environments. This bug was introduced in version 3.1b (2007/09/22) and fixed in version 3.1i (2008/04/13) of the caption package.

So updating your TeX distribution should help in this case.

If this is not your problem, please give us a MWE. (See http://www.latex-community.org/forum/vi ... =44&t=5838 , section 2.2: "Building an example")
drosa
Posts: 3
Joined: Sun Jan 01, 2012 10:31 pm

Reference to custom Counters

Post by drosa »

to reproduce the problem you need to download the AGU scientific paper template:

ftp://ftp.agu.org/journals/latex/journa ... agutex.cls
ftp://ftp.agu.org/journals/latex/journa ... /agu08.bst

and then this code:

Code: Select all

\def\documentstylefolder{/home/daniele/Downloads/journal}
\def\bibtexstylefolder{/home/daniele/Downloads/journal}

\newcounter{phase} \setcounter{phase}{0}
\renewcommand{\thephase}{\roman{phase}}
\newenvironment{phase}
{\refstepcounter{phase}{(\thephase})}

%\documentclass[11pt]{article}
\documentclass[jgrga]{\documentstylefolder/agutex}

\begin{document}
\begin{article}

ref to miao: \ref{miao}

ref to ciao: \ref{ciao}

item ciao: \phase\label{ciao}

\begin{figure*}[h]
\begin{tabular}{|l|}
item miao: \phase\label{miao} \\
\end{tabular}
\caption{this is the caption}
\label{fig-figure}
\end{figure*}

\end{article}
\end{document}
if i comment the call to those templates i don't have the error but i need the templates for submitting the scientific paper

i opened them but i got lost right away

thank you very much in case figure it out

best
daniele
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Reference to custom Counters

Post by sommerfee »

drosa wrote:to reproduce the problem you need to download the AGU scientific paper template:
This template redefines \label inside floating environments like figure in a way which does not work with other references than the one to the figure itself, i.e. \label only works correctly after \caption here.

But the original definition will be saved by this document class inside \savelabel, so you can use \savelabel inside figures for non-figure-labels, e.g.:

Code: Select all

%\def\documentstylefolder{/home/daniele/Downloads/journal}
%\def\bibtexstylefolder{/home/daniele/Downloads/journal}

\newcounter{phase} \setcounter{phase}{0}
\renewcommand{\thephase}{\roman{phase}}
\newenvironment{phase}
{\refstepcounter{phase}{(\thephase})}

%\documentclass[jgrga]{\documentstylefolder/agutex}
\documentclass[jgrga]{agutex}

\begin{document}
\begin{article}

ref to miao: \ref{miao}

ref to ciao: \ref{ciao}

item ciao: \phase\label{ciao}

\begin{figure*}[h]
\begin{tabular}{|l|}
item miao: \phase\savelabel{miao} \\ % <= This line changed
\end{tabular}
\caption{this is the caption}
\label{fig-figure}
\end{figure*}

\end{article}
\end{document}
drosa
Posts: 3
Joined: Sun Jan 01, 2012 10:31 pm

Re: Reference to custom Counters

Post by drosa »

Thank you!

best
daniele
Post Reply