2

I'm writing a thesis (using the book format on Latex), and some of my chapters happen to have appendixes. I would like each appendix to be numbered A, B, and so on, so I used the following code for each one of them:

\appendix
\section*{Appendix} 
\setcounter{theorem}{0}
\renewcommand{\thetheorem}{A.\arabic{theorem}}
\setcounter{equation}{0}
\renewcommand{\theequation}{A.\arabic{equation}}
\setcounter{figure}{0}
\setcounter{table}{0}
\renewcommand{\thetable}{A.\arabic{table}}
\renewcommand{\thesubsection}{A.\arabic{subsection}}

The problem is now all chapters are numbered with A, instead of using progressive arabic numbers...how can I limit the counters (and the reset of figure and table numbers) to the appendix section?

3

I did not create a theorem environment (and didn't load any packages to do so), so I commented the line adding the theorem counter to the list of counters to be redefined. If you need any more counters add them to the \Appendix@CounterList, just make sure to comment all the line endings to prevent adding a spurious space to any of the names. An empty list item is detected, so adding a completely empty element does no harm (so the comma after the last entry in the comma separated list is ok), while an element containing of a space wouldn't be detected.

Adding \appendix to the environment doesn't work out, because it does some redefinitions globally, the only alternative would be to save all the globally redefined macros in temporary ones and resetting them. Ask if you need this, I could add that (in a static manner or by redefining \gdef---which might be a bad idea). However the following might be a good start:

EDIT: I've added the numbering of appendix sections. Also I've added code which is able to undo any changes made by a \appendix (I hope). The relevant lines in the creation of Appendix (the environment) are commented, since I don't think it makes much sense adding \appendix as the changes made by it seem all to be not necessary with the section-approach.

\documentclass[]{report}

\usepackage[]{graphicx}
\usepackage{duckuments}

\newcounter{appendixcounter}

\makeatletter
\newcommand*\Appendix@Counter[1]
  {%
    \Appendix@SaveCounter{#1}%
    \setcounter{#1}{0}%
    \expandafter\Appendix@SaveTheCounter\expandafter{#1}%
  }
\newcommand*\Appendix@SaveTheCounter[1]
  {%
    \expandafter\def\csname the#1\endcsname{\Alph{appendixcounter}.\arabic{#1}}%
  }
\newcommand*\Appendix@SaveCounter[1]
  {%
    \expandafter\edef\csname Appendix@SaveCounter@#1\endcsname{\arabic{#1}}%
  }
\newcommand*\Appendix@ResetCounter[1]
  {%
    \setcounter{#1}{\csname Appendix@SaveCounter@#1\endcsname}%
  }
\newcommand*\Appendix@CounterList
  {%
    %theorem,%
    equation,%
    figure,%
    table,%
  }
\newcommand*\Appendix@CounterOnlyResetList
  {%
    chapter,%
    section,%
  }
\newcommand*\Appendix@GlobalUndoList
  {%
    thechapter,%
    @chapapp,%
  }
\newcommand*\Appendix@GlobalSave[1]
  {%
    \expandafter\Appendix@GlobalSave@i\expandafter{\csname #1\endcsname}{#1}%
  }
\newcommand*\Appendix@GlobalSave@i[2]
  {%
    \expandafter\let\csname Appendix@GlobalSave@#2\endcsname=#1%
  }
\newcommand*\Appendix@GlobalRestore[1]
  {%
    \expandafter\Appendix@GlobalRestore@i\expandafter
      {\csname Appendix@GlobalSave@#1\endcsname}{#1}%
  }
\newcommand*\Appendix@GlobalRestore@i[2]
  {%
    \expandafter\global\expandafter\let\csname #2\endcsname=#1%
  }
\newcommand*\Appendix@DoForListMacro[2]
  {%
    \expandafter\@for\expandafter\zz\expandafter:\expandafter=\expandafter
      {#1}\do
      {%
        \if\relax\detokenize\expandafter{\zz}\relax
        \else
          #2%
        \fi
      }%
  }
\newcommand*\Appendix@DoForCounterList
  {%
    \Appendix@DoForListMacro{\Appendix@CounterList}%
  }
\newcommand*\Appendix@DoForGlobalUndoList
  {%
    \Appendix@DoForListMacro{\Appendix@GlobalUndoList}%
  }
\newcommand*\Appendix@DoForCounterOnlyResetList
  {%
    \Appendix@DoForListMacro{\Appendix@CounterOnlyResetList}%
  }
\newenvironment{Appendix}
  {%
    \Appendix@DoForCounterList{\Appendix@Counter{\zz}}%
    \Appendix@DoForCounterOnlyResetList{\Appendix@SaveCounter{\zz}}%
    %\Appendix@DoForGlobalUndoList{\Appendix@GlobalSave{\zz}}%
    %\appendix
    \refstepcounter{appendixcounter}%
    \section*{Appendix \Alph{appendixcounter}}%
  }
  {%
    \Appendix@DoForCounterList{\Appendix@ResetCounter{\zz}}%
    \Appendix@DoForCounterOnlyResetList{\Appendix@ResetCounter{\zz}}%
    %\Appendix@DoForGlobalUndoList{\Appendix@GlobalRestore{\zz}}%
  }
\makeatother

\begin{document}
\chapter{Foo}
\blindduck[1,maths]
\begin{equation}
  E=mc^2
\end{equation}
\begin{Appendix}
  \blindduck[1,maths]
  \begin{figure}% >>>
    \centering
    \includegraphics{example-image-duck}%
    \caption{A friendly duck\label{fig:duck}}%
  \end{figure}% <<<
  \begin{table}
    \centering
    \begin{tabular}{ccc}
      Ducks&are&friendly
    \end{tabular}
    \caption{The truth\label{tab:truth}}
  \end{table}
\end{Appendix}
\begin{equation}
  E=mc^2
\end{equation}
\chapter{Bar}
\blindduck[1,maths]
\begin{Appendix}
  \blindduck[1,maths]
\end{Appendix}
\end{document}
| improve this answer | |
  • Thank you so much!! Now chapter numbering is finally retained. The only thing that is not working yet is that all appendices are labelled with A, instead of having A, B, C, and so on; probably I'm doing something wrong, but I don't understand what it is – Mary Aug 30 '18 at 15:15
  • @Maria that is my fault, as I did not include a counter for the appendices. I thought you wanted all of them be labelled with A. How do you want them to be labelled? Should those appendices be sections or chapters? – Skillmon likes topanswers.xyz Aug 30 '18 at 15:22
  • They should be sections; basically the last section of chapter 2 of my thesis is an appendix, and it should be labelled A; the last section of chapter 3 is an appendix as well, and should be labelled B – Mary Aug 30 '18 at 15:33
  • @Maria I'll post some stuff in a minute. – Skillmon likes topanswers.xyz Aug 30 '18 at 15:38
  • @Maria see my edit, I hope it adds everything you need. – Skillmon likes topanswers.xyz Aug 30 '18 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.