[Tex/LaTex] Roman numeral of defined counter

cross-referencingroman numerals

I defined myself a counter for Examples and want it to be displayed in uppercase Roman numerals.

\newcounter{exampleCounter}
\newenvironment*{Example}{\refstepcounter{exampleCounter}~\\ \noindent \textbf{Example\Roman{exampleCounter}}~\\ \begin{itshape}}{\end{itshape}}

This works perfectly fine, but when I am trying to reference an example, I am just recieving arabic numbers.
I tried

\uppercase\expandafter{\romannumeral 0\ref{blah}}

(a solution I found here but this stopped working, when I needed ö,ä,ü in my text.

At first, it was not finding the lowercase references, I got to workaround by CAPSLOCKING the labels, but roman numbering is still not possible.

Working Example

\documentclass{report}

\usepackage[ngerman,english]{babel}
\usepackage[utf8]{inputenc}

\newcounter{exampleCounter}
\newenvironment*{Example}{\refstepcounter{exampleCounter}~\\ \noindent \textbf{Example \Roman{exampleCounter}}~\\ \begin{itshape}}{\end{itshape}}

\begin{document}

\begin{Example}
\label{EX:BLAH}
    This is an Example, I want to reference
\end{Example}

\noindent
Arabic example \ref{EX:BLAH}    \\
Roman example \uppercase\expandafter{\romannumeral 0\ref{EX:BLAH}}

\end{document}

Does anyone has an idea, how to get this roman numbers back in?

Best Answer

When you define a counter, LaTeX performs two actions. It creates the actual counter by assigning it to a register, but it also creates a counter representation macro. For some counter <cntr>, the latter is given by \the<cntr>. Internally, this is handled by the macro \@definecounter (from latex.ltx, with comments added):

\def\@definecounter#1{\expandafter\newcount\csname c@#1\endcsname% <- Defined \c@<cntr>
     \setcounter{#1}\z@% <------------------------------------------- Sets counter to 0
     \global\expandafter\let\csname cl@#1\endcsname\@empty% <-------- Clear/reset list \cl@<cntr>
     \@addtoreset{#1}{@ckpt}%
     \global\expandafter\let\csname p@#1\endcsname\@empty% <--------- Reference prefix \p@<cntr>
     \expandafter
     \gdef\csname the#1\expandafter\endcsname\expandafter% <--------- Sets \the<cntr>
          {\expandafter\@arabic\csname c@#1\endcsname}}%              to be \arabic{<cntr>}

So, for your purposes, the use of

\newcounter{Abc}

LaTeX creates

\c@Abc
\theAbc

(and some other stuff). By default, \the<cntr> is set to \arabic{<cntr>}. But you can redefine that the way you want to. Specifically, in your case, use \Roman{<cntr>} and use \the<cntr> elsewhere.

enter image description here

\documentclass{article}

\newcounter{exampleCounter}
\renewcommand{\theexampleCounter}{\Roman{exampleCounter}}
\newenvironment*{Example}
  {\refstepcounter{exampleCounter}% \begin{Example}
   \par\addvspace{\topsep}%
   \noindent\textbf{Example~\theexampleCounter}\par%
   \itshape\ignorespaces\noindent}
  {\par\addvspace{\topsep}%
   \ignorespacesafterend}% \end{Example}

\begin{document}

\noindent Here is some regular text.
\begin{Example}\label{EX:BLAH}%
This is an Example, I want to reference
\end{Example}
This is a reference to Example~\ref{EX:BLAH}.
\end{document}

It's important to change \the<cntr> since that's exactly what is used when you make a \label - LaTeX stores in the label/reference the value of the last \the<cntr> that was stepped.