Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to create a referenceable "system" environment that basically displays an equation with a custom counter. Then I want to reference the environment with autoref, so as to have "System (S1)" as output when I reference it.

\newcounter{system}\setcounter{system}{0}

\newenvironment{system}{\refstepcounter{system}\begin{equation*}}{\hfill(S\arabic{system})
\end{equation*}}

\hyperref[system]{System~\ref{system}}
\def\systemautorefname{System}

\begin{system}
 \dot x=u(x),
\label{s1}
\end{system}

However, when I do

\autoref{s1}

I get an error saying that the reference s1 is undefined. What am I missing?

share|improve this question
add comment

migrated from stackoverflow.com Nov 12 '12 at 21:59

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 9 down vote accepted

Some remarks:

  • The label has used the name s1, the references system. The label name must match, the example below uses s1.
  • The use equation* indicates that probably package amsmath is used.
  • With amsmath \tag can be used to print the equation number.
  • Redefining \p@system helps to get the parentheses around the number, when it is referenced.
  • I have put \label outside environment equation with the name as option to the environment system to avoid trouble with the redefinition of \label inside environments of amsmath.
  • The star form of \ref avoids the link inside a link in the line with \hyperref.

Example:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}

\newcounter{system}
\renewcommand*{\thesystem}{S\arabic{system}}
\newcommand*{\systemautorefname}{System}
\makeatletter
\renewcommand*{\p@system}{%
  \expandafter\p@@system
}
\newcommand*{\p@@system}[1]{%
  (#1)%
}
\makeatother

\newenvironment{system}[1][]{%
  \refstepcounter{system}%
  \ifx\\#1\\%
  \else
    \label{#1}%
  \fi
  \begin{equation}%
}{%
  \tag{\thesystem}%
  \end{equation}%
}

\begin{document}

\hyperref[s1]{System~\ref*{s1}}, \autoref{s1}

\begin{system}[s1]
 \dot x=u(x),
\end{system}

\end{document}
share|improve this answer
1  
Thank you so much! I probably never could have written that from scratch, but I've already learned a lot just from backwards engineering your code. Didn't even know there was a starred version of \newcommand until now. Probably don't even need to mention that it works like a charm. –  user1816518 Nov 12 '12 at 23:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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