1

I'm using subfig package for sub-figures, thus I'm trying to create a scheme to add a enumeration inside the label to make it easier to describe the sub-pictures. Using \begin{enumerate*} and \begin{inparaenum} is tedious because I need to instantiate them everytime, I tried to use a \newcounter but I don't know why it gives lots of csname error when I put it inside caption and use refstepcounter{x}.

Then I came accross pure TeX \newcount and it gives no error within caption

I have developed my own minimal functional example

\newcount\cnt
\cnt=1
\newcommand{\slb}{(\textit{\@alph\cnt}){ }\advance\cnt by 1 }

\newcommand{\slrt}{\cnt 1 \relax}
\newcommand{\lbl}[1]{\label{#1}\slrt}

Where \slb shows an increasing numbering alph and \slrt resets the numbering, I then wrapped label command inside my own \lbl which resets it by the end of a figure.

However it would be more accurate if it could behave like a LaTeX counter with \addtoreset feature to it.

Is it possible to add such a feat? And if yes, how so?

EDIT: minimal working example What I'm trying to do is to remove the hassle to create a enumeration inside the caption tag

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{caption}

\begin{document}
\begin{figure*}[!htb]
  \centering
  \subfloat[]{
    \includegraphics[height=0.18\textwidth]{example-image-a}%
    \label{fig:sub-0}}
  ~
  \subfloat[]{
    \includegraphics[height=0.18\textwidth]{example-image-b}%
    \label{fig:sub-1}}
  ~
  \subfloat[]{
    \includegraphics[height=0.18\textwidth]{example-image-c}%
    \label{fig:sub-2}}
\caption[Example]{Example: (\textit{a}) I'm trying, 
                           (\textit{b}) to make 
                           (\textit{c}) this enumerating easier.}
\label{fig:ex-0}
\end{figure*}
\end{document}

Where I simply replace the (\textit{a}) with \slb

5
  • 5
    It should be possible, since LaTeX counters are abstractions on top of \counts. However it's probably more work than it's worth. If you just want to put \refstepcounter{x} inside a caption, then use \protect\refstepcounter{x}. However you'll get weird results, because this \refstepcounter will go to the list of figures and your counter will increase there as well. Better use \caption[List of figures caption]{Document caption and \refstepcounter{x}}. Commented Nov 26, 2019 at 0:09
  • 4
    It would be far better if you could provide the full context in the form of a minimal working example. \captions are fragile and there might be a better way to achieve what you're after.
    – Werner
    Commented Nov 26, 2019 at 0:13
  • 3
    why not simply use \newcounter as adding the missing definitions to make the reset system work is exactly equivalent to using the latex defintion Commented Nov 26, 2019 at 0:50
  • 2
    you do not want \newcount or \newcounter inside a \caption you do not want to allocate a new register every caption, just allocate it once in the preamble (although sub figures already have a counter so it isn't clear what you aim to do at all to be honest) Commented Nov 26, 2019 at 8:02
  • I have provided a minimal working example
    – richard_ba
    Commented Nov 26, 2019 at 23:50

1 Answer 1

2

Just use a regular \newcounter that is reset with every use of \caption inside a figure. Then \slb can just step this counter and set it. Here's an example:

enter image description here

\documentclass{article}

\usepackage{graphicx}
\usepackage{subfig}
\usepackage{caption}

\captionsetup{
  justification = centering,
  singlelinecheck = false
}

\newcounter{subfigcntr}[figure]
\renewcommand{\thesubfigcntr}{(\textit{\alph{subfigcntr}})}
\newcommand{\slb}{\stepcounter{subfigcntr}\thesubfigcntr~}

\begin{document}

\begin{figure*}
  \centering
  \subfloat[]{\includegraphics[height=0.18\textwidth]{example-image-a}}
  ~
  \subfloat[]{\includegraphics[height=0.18\textwidth]{example-image-b}}
  ~
  \subfloat[]{\includegraphics[height=0.18\textwidth]{example-image-c}}
  \caption[Example]{Example: \slb I'm trying, 
                             \slb to make 
                             \slb this enumerating easier.}
\end{figure*}

\begin{figure*}
  \centering
  \subfloat[]{\includegraphics[height=0.18\textwidth]{example-image-a}}
  ~
  \subfloat[]{\includegraphics[height=0.18\textwidth]{example-image-b}}
  ~
  \subfloat[]{\includegraphics[height=0.18\textwidth]{example-image-c}}
  \caption[Example]{Example: \slb I'm trying, 
                             \slb to make 
                             \slb this enumerating easier.}
\end{figure*}

\end{document}
2
  • Ok, this approach works, however \subfloat[] requires \centering to be centered, any way to make this the default behavior?
    – richard_ba
    Commented Nov 27, 2019 at 20:37
  • @richard_ba; Yes. Add justification = centering to your \captionsetup. I've updated the answer and removed the manual use of \centering.
    – Werner
    Commented Nov 27, 2019 at 21:36

You must log in to answer this question.

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