Question summery:
How to change my implementation, that counter can print 1.1 , 1.2a, 1.2b , 1.3 where the number before the point is the section number an the part after the point is a own counter?
Question in long: I would like to tweak my command for generating an "exercise counter". First I show you, how I want to use my command, and then I am trying to describe the wanted behavior. At the end I show you my current impementation, which does not what I want:
\section{Bla}
\exerciseOpt[a]{test}\label{ex1}
[...]
After \ref{ex1} we can jump to \ref{ex2} and [...]
\exerciseOpt[b]{test}\label{ex2}
[...]
\exerciseOpt{test}\label{ex3}
The "After"-line should printed like:
After 1.1a we can jump to 1.1b and [...]
While the command \exerciseOpt should print a "headline" bounded at section number and an own number and showing the optional parameter as textual postfix like:
Exercise 1.1a test
and on the second call
Exercise 1.1b test
while the third call should give
Exercise 1.2 test
I have found similar questions but their point are to different to my problem or didn't cover enought information for me:
- Define Custom Text with a Counter
- \ref with custom text
- Cross-Reference with custom text
- Getting a proper reference number with \ref while using custom counter and environment
And here is my command implementation, which doesn't handle the counter as I wanted:
\newcounter{excounter}[section]
\renewcommand{\theexcounter}{\thesection.\arabic{excounter}}
\newcommand{\showexcounter}{\theexcounter}
\newcommand{\exerciseOpt}[2][\relax]{%
\def\myOptional{#1}
\refstepcounter{excounter}
\if\myOptional\relax
\textbf{Exercise: \showexcounter : #2}%
\else
\textbf{Exercise: \showexcounter\myOptional : #2}%
\fi
}
An "working minimal code example" which shows, that the numbers are not like I want to be in \ref output and directly in command \exerciseOpt, too
\documentclass{article}
\newcounter{excounter}[section]
\renewcommand{\theexcounter}{\thesection.\arabic{excounter}}
\newcommand{\showexcounter}{\theexcounter}
\newcommand{\exerciseOpt}[2][\relax]{%
\def\myOptional{#1}
\refstepcounter{excounter}
\if\myOptional\relax
\textbf{Exercise: \showexcounter : #2}%
\else
\textbf{Exercise: \showexcounter\myOptional : #2}%
\fi
}
\begin{document}
\section{Bla}
\noindent{}\exerciseOpt[a]{test number should be 1.1a}\label{ex1}
\noindent{}After \ref{ex1} we can jump to \ref{ex2} and [...]\newline
\noindent{}it should be: After 1.1a we can jump to 1.1b and [...]
\noindent{}\exerciseOpt[b]{test number should be 1.1b}\label{ex2}
\noindent{}\exerciseOpt{test number should be 1.2}\label{ex3}
\section{blub}
\noindent{} \exerciseOpt{test number should be 2.1}\label{ex4}
\end{document}