R2B2
R2B2

Reputation: 1591

Latex, "No counter 'counterName' defined" error, using \include

I get the following error:

! LaTeX Error: No counter 'exCoutner' defined.

when I try to compile this code:

\documentclass[]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}

\usepackage{amsmath}

\newcounter{exCounter}
\numberwithin{exCounter}{section}
\newenvironment{example}[1]{
    \begin{center}
    \begin{minipage}[t][\height][c]{0.8\textwidth}
    \hrulefill\newline
    \refstepcounter{exCoutner}
    \textit{Example \arabic{exCounter}} - \textbf{#1}\newline
    }{
    \vspace{-0.5\baselineskip}
    \hrulefill
    \end{minipage}
    \end{center}
    \vspace{\baselineskip}
    }

\begin{document}

\include{chapter1}

\include{chapter2}

\include{chapter3}

\end{document}

The chapterX.tex files follow this pattern:

\section{Addition}

\begin{example}{Addition}
$1 + 1 = 2$
\end{example}

When commenting the line

\refstepcounter{exCounter}

everything works fine, but obviously the counter does not increase, and "Example 0" is printed in each time I use the environment "example".

What seems strange to me is that no error at all is thrown when commenting the line, and LaTeX manage to access exCounter (since it prints the value 0), but as soon as I try to increment it (even using \addtocounter), this "no counter defined" error appears.

Thank you very much!

Upvotes: 7

Views: 10328

Answers (1)

David Gorsline
David Gorsline

Reputation: 5018

Perhaps

\refstepcounter{exCoutner}

should be

\refstepcounter{exCounter}

Upvotes: 6

Related Questions