7

Problem: I want to create an environment named Example, and the environment has a counter (number within chapter). I want to refer to the counter for illustration just as the table, figure and equation environment.

When using \newenvironment, the command \refstepcounter works. However, since the new environment includes a TikZ code, I can not use \newenvironment to create the environment. Alternatively, I used environ package. The only problem is that the cross-reference of the counter doesn't work, even though I use \refstepcounter in my customized environment.

Example Minimal Example is given as follows:

\documentclass{book}

\usepackage{lipsum}
\usepackage{mathpazo}

\newcounter{exam}[chapter]
\setcounter{exam}{0}
\renewcommand{\theexam}{\thechapter.\arabic{exam}}

% % Method 1

% \newenvironment{example}[1][]{\refstepcounter{exam}\par\medskip\noindent%
%    \textbf{Example \theexam~ #1} \rmfamily}{\medskip}

% Method 2
\usepackage{environ,tikz}
\tikzstyle{title}=[right=10pt,fill=gray,text=green!50]
\tikzstyle{context}=[thick,rectangle,draw=gray,inner sep=10pt, inner ysep=10pt]
\NewEnviron{example}[1][{}]{%
\par\noindent\centering
\begin{tikzpicture}
    \node[context](box){
        \begin{minipage}{0.93\textwidth}
            \BODY
        \end{minipage}};
    \node[title] at (box.north west){
    \refstepcounter{exam}\par\medskip\noindent \textbf{Example \theexam~ #1}};
\end{tikzpicture}}

\begin{document}

\setcounter{chapter}{2}

\begin{example}[First]\label{exam:1}
    \lipsum[4]
\end{example}

\lipsum[2]

\begin{example}[Second]\label{exam:2}
  \lipsum[5]
\end{example}

Example ~\ref{exam:1} and ~\ref{exam:2} are the new environments!

\end{document}

Method 1 works, but method 2 does not work. How can I make it work?

Additional information: if possible I would try to not use ntheorem or tcolorbox.

Thank you!

1
  • Thank you very much! You are so nice! Actually, I often browse TeX.SX. Since I am not a native English speaker, I seldom ask question on the website. However, I have learned much skills from TeX.SX when using LaTeX, when I encounter a problem, I can always find a solution. It's so wonderful, and TeX Users on TeX.SX are brilliant and nice! Apr 15, 2014 at 15:20

1 Answer 1

3

\refstepcounter should be used before \label. \refstepcounter defines \@currentlabel that is used by \label.

\documentclass{book}

\usepackage{lipsum}
\usepackage{mathpazo}

\newcounter{exam}[chapter]
\setcounter{exam}{0}
\renewcommand{\theexam}{\thechapter.\arabic{exam}}

% % Method 1

% \newenvironment{example}[1][]{\refstepcounter{exam}\par\medskip\noindent%
%    \textbf{Example \theexam~ #1} \rmfamily}{\medskip}

% Method 2
\usepackage{environ,tikz}
\tikzstyle{title}=[right=10pt,fill=gray,text=green!50]
\tikzstyle{context}=[thick,rectangle,draw=gray,inner sep=10pt, inner ysep=10pt]
\NewEnviron{example}[1][{}]{%
    \par
    \centering
    \addvspace{\medskipamount}%
    \begin{tikzpicture}
        \node[context](box){%
            \begin{minipage}{0.93\textwidth}
                \refstepcounter{exam}%
                \BODY
            \end{minipage}};
        \node[title] at (box.north west){\textbf{Example \theexam\ #1}};
    \end{tikzpicture}%
    \par
}

\begin{document}

\setcounter{chapter}{2}

\begin{example}[First]\label{exam:1}
    \lipsum[4]
\end{example}

\lipsum[2]

\begin{example}[Second]\label{exam:2}
    \lipsum[5]
\end{example}

Example~\ref{exam:1} and~\ref{exam:2} are the new environments!

\end{document}

Result

Remarks:

  • Example ~\ref{...} sets two spaces and the first allows a line break. Use ~ instead of a space: Example~\ref{...}.

  • \par\noindent\centering: TeX uses the settings at the end of a paragraph. However the group is closed before the paragraph is ended, thus \centering does not center the example box. The example above closes the paragraph right after \end{tikzpicture}. Then the centering works and \noindent is not needed (\parindent is set to zero by \centering).

  • I have moved \medskip at the start of the example. Inside a node's \hbox it does not add vertical space.

If you want to have the full line width for the example environment, then the width for the minipage environment can be calculated:

\begin{minipage}{%
    \dimexpr
        \linewidth % available text width
        -\pgfkeysvalueof{/pgf/inner xsep}*2 % inner margin of the frame
        -2\pgflinewidth % rule width of the frame
    \relax   
}%
1
  • Thank you very much(×N)!!! I spent all the night looking for solutions, but unfortunately, I got nothing! I can have a nice dream tonight, lol! I'd appreciate it that you point out some mistakes for me! Remarks and supplementary are useful! Apr 15, 2014 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .