# New counter that can be referenced

I have created a method \makeExample that creates a new counter called example. Can you tell me how I can modify it, so that I can make references to the example counter? So that I for example can write: some text.... \vref{label} .....

A working sample can be found here: https://www.sharelatex.com/project/541ffc3e05c84a5c4060ae58

My method looks like this:

\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
\LARGE\textsc{Eksempel~\thechapter.\theexample #1}}{\medskip}

\newcommand\makeExample{%
{%
\begin{center}%
\begin{example}%
\end{example}%
\end{center}%
}%
}

• Just put a \label in the body of the example. – Andrew Swann Sep 22 '14 at 10:13
• How can I do that when using the '\makeExample' command? – 7heViking Sep 22 '14 at 10:22
• Where do you put the body of the example? Why don't you just define an environment for this instead? – Andrew Swann Sep 22 '14 at 10:26
• Because the content of the environment is changed. E.g. the font size differes from outside of the environment. – 7heViking Sep 22 '14 at 10:31
• Can you please provide an example of the usage - a minimal working example (MWE) would be most helpful – Andrew Swann Sep 22 '14 at 10:36

The simplest modification to your code is to add an optional argument to \makeExample where you can put arbitrary code, such a \label. I post an example of this at the bottom, however I strongly suspect this not what you want. The sample you provide is setting up an mdframed enviornment and it would be easiest to use the theorem-like mechanism provide by the mdframed package as follows:

\documentclass{memoir}

\usepackage[utf8]{inputenc}
\usepackage{mathtools,varioref}
\usepackage[framemethod=TikZ]{mdframed}

\mdfdefinestyle{cexamplestyle}{%
frametitlefont=\normalfont\Large\scshape,
frametitlealignment=\center
}
\mdtheorem[style=cexamplestyle]{centerexample}{Example}[chapter]

\begin{document}

\vref{todo} is a sample. The fist is a sample of how I would like it to be

\begin{centerexample}
\label{todo}
\noindent
\textbf{Data:} 1011
\newline
\textbf{Function:} $a\cdot x^3+b\cdot x^2+c\cdot x+d\cdot 1$

\noindent
Værdierne fra $a$, $b$, $c$ og $d$ findes som følger:
\newline
$a=1$, $b=0$, $c=1$ og $d=1$
\end{centerexample}

\end{document}


Here is the result the first modification would have given:

\documentclass{book}

\usepackage{mdframed}
\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
\LARGE\textsc{Example~\thechapter.\theexample #1}}{\medskip}

\newcommand{\makeExample}[1][\relax]{%
{%
\begin{center}%
\begin{example}%
#1%
\end{example}%
\end{center}%
}%
}

\begin{document}

\chapter{A chapter}
\label{cha:chapter}

\begin{mdframed}
\makeExample[\label{cex:sample}]
A centered example.
\end{mdframed}

That was Example~\ref{cex:sample}.

\end{document}


I think the first approach is much simpler.

• Your answer is the closest one. However, as your image also shows the reference number is "1" bout it should be "1.1". How can this be corrected? – 7heViking Sep 22 '14 at 12:11
• The code did produce the correct output, it is just that I had a reference to the wrong label! Now fixed with corresponding image. – Andrew Swann Sep 22 '14 at 12:34
• The first solution you provided Works perfectly :) It just took me a while to realize that the \mdfdefinestyle{cexamplestyle} actually was the mdframed style... dont ask me why :S – 7heViking Sep 23 '14 at 7:34

When asking questions you should provide a full minimal working example (MWE), both in order to show what you are trying to do and to help others help you -- it is much easier to help some one if they give you something to work from. The MWE should start with a \documentclass statement, contain all of the packages that you want and then have \begin{document}...\end{document}. The MWE should compile and contain close to the minimal amount of code needed to explain/demonstrate what you are asking. This saves a lot of time for everyone.

In this case, it seems to me that you are simply trying to define an environment for displaying examples. If this is what you are trying to do then you are working too hard as you can just use \newtheorem{example}[section]{Eksempel}. A full MWE, complete with cross-referencing, is

\documentclass{article}
\usepackage{amsmath}
\newtheorem{example}{Eksempel}% implicitly defines a counter
\numberwithin{example}{section}% numbers it within sections
\begin{document}
\section{Important examples}
\begin{example}\label{MyExample}% \label creates the reference
1+1=2.
\end{example}

My Eksempel~\ref{MyExample} is great.% \ref creates the cross-reference

\end{document}


This produces:

Look at the documentation for the amsthm package to see how you can customise the theorem-like environments.

Edit As I mentioned to customise the style of Eksempel you should read the documentation for the amsthm package. If you add the lines:

\usepackage{amsmath,amsthm}
\newtheoremstyle{eksempel}
{3pt}% ⟨Space above⟩
{3pt}% ⟨Space below⟩
{}% ⟨Body font⟩
{0pt}% ⟨Indent amount⟩
{\scshape}% ⟨Theorem head font⟩
{}% ⟨Punctuation after theorem head⟩
{0pt}% ⟨Space after theorem head⟩2
{\centerline{\thmname{#1} \thmnumber{#2}}}% ⟨Theorem head spec⟩
\theoremstyle{eksempel}
\newtheorem{example}{Eksempel}% implicitly defines a counter


then you get the style that you were trying to define:

Note that the \hfills for the "theorem head spec" centre the Eksempel heading.

If you are trying to do more than this then your code is still trying too hard because you do not need to define both the \newenvironment{example} and the \makeExample commands: environments are themselves commands. In this case you probably want something like

\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
\center\LARGE\textsc{Eksempel~\thechapter.\theexample #1}}
{\endcenter\medskip}


(I suggest deleting the \par and \medskips, however, as they look wrong to me.)

As you are using \refstepcounter you can use this command as in the MWE above with \label and \ref to create cross-references. This said, it's probably better to use \theexample in the definition of the environment and to add:

\renewcommand\theexample{\thechapter.\arabic{example}}


Finally, since you define the example counter to be a subcounter of section, I suspect that you really want:

\renewcommand\theexample{\thesection.\arabic{example}}

• Perfect. Just one comment. How would you go by centering the example text? So that "Eksempel 2.1" would be centered on the very first line with no other text. And so that it doesn't include any more than "Eksempel [chapter number][example number]. – 7heViking Sep 22 '14 at 11:55
• @FireFly3000 As I mentioned, you just need to read the documentation for amsthm:) I've added the details. – Andrew Sep 22 '14 at 20:45

This is a version with an automated label generation, but however, this might fail after resetting of the counter.

\documentclass{book}

\newcounter{example}[section]

\renewcommand{\theexample}{\thesection.\arabic{example}}
\newenvironment{example}[1][]{\refstepcounter{example}\label{mysophisticatedexample::\number\value{example}}
\LARGE\textsc{Eksempel~\theexample #1}\par\medskip

}{\medskip}

\newcommand\makeExample{%
{%
\begin{center}%
\begin{example}%
This is some dummy example with \theexample as counter and it references \ref{mysophisticatedexample::1}
\end{example}%
\end{center}%
}%
}

\begin{document}

\chapter{First}

\section{First}

\makeExample

\makeExample

\makeExample

\end{document}