1

I defined my own environment for short pseudo-algorithms, and I would like to use the \labelformat command to change the output of \vref. Varioref's documentation says:

"The command takes two arguments: the first is the name of a counter and the second is its representation when referenced. This means that for a successful usage, one has to know the counter name being used for generating the label, though in practice this should not pose a problem. The current counter number is picked up as an argument."

This works for sections, but does not for my own environment.

MWE:

\documentclass{book}
\usepackage{amsmath}
\usepackage{varioref}

\labelformat{section}{Section~#1}
\labelformat{algctr}{Algorithm~#1}

% own environment for short algorithms
\newcounter{algctr}

\newenvironment{algo}[1]{           
    \medskip\noindent%
    \refstepcounter{algctr}%
    \textbf{Algorithm \thealgctr: #1}\\%
    \begin{minipage}{\textwidth}%
        \leftskip=1.5em%
}{\end{minipage}\par\medskip}%
\numberwithin{algctr}{chapter}

\begin{document}
\section{Testsection}
\label{ex1}
Reference to section: \vref{ex1}, reference to algorithm: \vref{ex2}.
\begin{algo}{Example}\label{ex2}
This is an example.
\end{algo}

\end{document}

Output:

wrong output

2

The \labelformat command offers a way to change the counter behavior but for that to work the counter needs to exist. As you define the counter later that definition reinstalls the default behavior as neither check if there is already something defined.

So if you move the counter declaration before the \labelformat everything works out fine (well more or less you also need to add a \par into your environment definition as \medskip in LaTeX does not start vertical mode but uses \vspace) but then you get:

enter image description here

4
  • The solution was so simple that I'm a little embarrased I did not find it myself. I guess it got pretty late that night :) It works now, but I don't get you second part about \medskip - what do you mean, where do I need to add a \par? Nov 9 '14 at 14:37
  • 1
    @causaprima what do you think happens in "\par Test A \medskip Test B\par" ? where does the \medskip go and how does the result look like? Nov 9 '14 at 15:24
  • I guess you are referring to the first line in my enviroment definition, that should rather be \par\medskip\noindent%, right? Nov 9 '14 at 20:47
  • @causaprima yep Nov 9 '14 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.