pdflatex: Appendix section number is wrong - tex

The appendix section number is wrong. The whole project is at:
https://github.com/JerryOpenix/Debugging-With-GDB
MWE is below:
The section number of Appendix B is wrong.
Don't tell me to set the section number of Appendix B to be 0 at the start manually.
\documentclass[12pt,twoside,a4paper,openright]{book}
\usepackage[subpreambles=false]{standalone}
\usepackage[toc, page]{appendix}
\begin{document}
\pagenumbering{arabic}
\appendix
\renewcommand{\thechapter}{A\alph{chapter}}
\chapter*{Appendix A Installing GDB}
\addcontentsline{toc}{chapter}{Installing GDB}
\section*{tools to build gdb}
\#TODO
\setcounter{section}{0}
\section{call configuration script}
\#TODO
\renewcommand{\thechapter}{B\alph{chapter}}
\chapter*{Appendix B GDB protocol}
\addcontentsline{toc}{chapter}{GDB protocol}
\section{Overview}
\#TODO
\cleardoublepage
\end{document}`

Your example code is somewhat contrived (and I wasn't going to dive into compiling an entire project from GitHub). Here's the gist of what's happening:
You're using \chapter* to represent every appendix chapter. The starred version of \chapter implies that no counter will be set in the chapter heading. It also implies that the chapter counter will not be stepped (increased). The consequence from not stepping the chapter counter is that none of the lower-level sectional units have their counters reset. That's why the first section in the second appendix carries on from the first section in the first appendix.
You could attempt to add the following to your preamble:
\let\oldappendix\appendix% Store \appendix in \oldappendix
\renewcommand{\appendix}{% Update \appendix to...
\oldappendix % ...call the traditional \appendix
\let\oldchapter\chapter% Store \chapter in \oldchapter
\renewcommand{\chapter}{% Update \chapter to...
\setcounter{section}{0}% ...reset the section counter and...
\oldchapter% ...call the traditional \chapter macro
}%
}
The above code will update \appendix to do what it always does, but also add an update to \chapter. This update will reset the section counter in case you use \chapter*. Even if you were to use \chapter, it wouldn't matter.

Related

Appendix after references not in toc and figure label missing

I am trying to set up my Appendix for my master's thesis which includes basically only figures. These will be referenced at the end of the "List of figures".
My intention is to have figures called "Fig. A.1", "Fig. A.2" etc.
Also, the appendix should be part of the table of contents.
This works fine, as long as it appears before the bibliography.
However, the appendix should appear afterwards, and as soon as I move it there, the Figures are only called "Fig. .1" and it disappears from the TOC. (Note that the figures still show up in the list of figures, however with the wrong names).
How can I reach the same result while having the Appendix BELOW the references?
\documentclass[a4paper, 12pt]{article}
\usepackage{fancyhdr}
\renewcommand{\figurename}{Fig.}
\begin{document}
\tableofcontents
\fancyhead[EC]{\large REFERENCE LIST}% page header is always ``Reference List''
\fancyhead[OC]{\large REFERENCE LIST}%
\section* {Reference List} %removes the section from sections numbers and TOC
\addcontentsline {toc}{section}{Reference List} %adds the section to TOC
\bibliographystyle{THESIS2} %my custom bibliography style
\renewcommand{\section}[2]{} %removes superfluous ``References'' title
\bibliography{Literatur} %calls my .bib file
\newpage
\fancyhead[EC]{\large APPENDIX}
\fancyhead[OC]{\large APPENDIX}
\addcontentsline {toc}{section}{Appendix}
\appendix
\section{Appendix}
\end{document}
Thanks for your help!

How can I check in latex whether a new chapter starts on current page?

I am using the everypage package. Using the \AddEverypageHook command I can repeat actions at the beginning of every page of a document. Now I want to do something like this:
\AddEverypageHook{
\if "New chapter starts at current page." - "Do stuff."
\else "Do other stuff."
\fi
}
How can I check in latex, whether a new chapter starts at the current page?
In a typical document, the issue of a \chapter command is followed by an automated page break. For example, see what \chapter does in report.cls:
\newcommand\chapter{\if#openright\cleardoublepage\else\clearpage\fi
\thispagestyle{plain}%
\global\#topnum\z#
\#afterindentfalse
\secdef\#chapter\#schapter}
It issues a \clearpage (or \cleardoublepage), flushing whatever is pending and starting on a new page.
So, depending on your setup, it may suffice to use the afterpage package's \afterpage{<stuff>} macro to execute <stuff> after the current page. For example, in your preamble, you would
\let\oldchapter\chapter % Store \chapter
\renewcommand{\chapter}{% Redefine \chapter to...
\afterpage{\customcommand}% ...execute \customcommand after this page
\oldchapter}
Of course, this would only make sense if you don't execute anything else on non-chapter pages since the condition is tied to \chapter. So, making a document-wide decision at every page might require a slightly different approach.
I'd still suggest tapping into the \chapter macro, but using a conditional. Here is an example (click to enlarge image):
\documentclass{report}
\usepackage{lipsum,afterpage,everypage}
\newcounter{chapterpage}% For this example, a chapterpage counter
\newcounter{regularpage}% For this example, a regularpage counter
\newif\ifchapterpage% Conditional used for a \chapter page
% Just for this example, print page number using:
\renewcommand{\thepage}{\LARGE\thechapterpage--\theregularpage}
\AddEverypageHook{
\ifchapterpage % If on a \chapter page...
\stepcounter{chapterpage}% Increase chapterpage counter
\global\chapterpagefalse% Remove conditional
\else % ...otherwise
\stepcounter{regularpage}% Increase regularpage counter
\fi
}
\let\oldchapter\chapter % Store \chapter
\renewcommand{\chapter}{% Redefine \chapter to...
\afterpage{\global\chapterpagetrue}% ... set \ifchapterpage to TRUE _after_ this page
\oldchapter}
\begin{document}
\chapter{First chapter}\lipsum[1-50]
\chapter{Second chapter}\lipsum[1-50]
\chapter*{Third chapter}\lipsum[1-50]
\chapter{Final chapter}\lipsum[1-50]
\end{document}
The advantage of the above method is that it works for both \chapter and \chapter*. \chapter* doesn't increment the chapter counter and it is therefore insufficient to rely on a condition based on such a comparison.
I have a solution. The idea is to check, whether the chapternumber counter has changed. This is done by a custom counter:
\newcounter{CurrentChapNum}
if the chapter-counter did not change, we are still in the current chapter, so do \customcommand1 stuff.
if the counter has changed, we seem to be at the beginning of a new chapter, so do \customcommand2 stuff and reset the CurrentChapNum-counter to the value of the current chapter.
This is done by this code.
\AddEverypageHook{
\ifnum\value{chapter}=\value{CurrentChapNum} \customcommand1
\else \customcommand2 \setcounter{CurrentChapNum}{\value{chapter}}
\fi
}
Since I am quite new to latex-markup-stuff I hope this is not too clumsy.

Redefining sectioning commands in latex, issues with references and labels

I'm trying to change the appearance of one of the native sectioning commands in LaTeX. But after doing so, latex cannot handle references as expected.
The code given later is expected to output a document with the text
1 One
See section 2.
2 Two
See section 1.
But instead I get the following.
1 One
See section .
2 Two
See section .
What can I change in the renewed command, such that the references will work again.
The code for the document is as follows:
\documentclass{article}
\newcounter{seccnt}
\renewcommand{\section}[1]{\vspace{2em}\stepcounter{seccnt} \theseccnt~ {\Large #1}\vspace{0.5em}}
\begin{document}
\section{One}
\label{secOne}
See section \ref{secTwo}.
\section{Two}
\label{secTwo}
See section \ref{secOne}.
\end{document}
I think you need to use \refstepcounter instead of \stepcounter, so that the reference is stored.

Adding bibliography to appendices in latex

I am writing my thesis and I am using the chapterbib option. While it makes beautiful bibliographies for my chapters, I can't get it to do the same thing for my appendices.
The preamble:
\documentclass[pdftex, 11pt, onecolumn, openany]{report}
\usepackage{amsmath}
\usepackage[pdftex]{graphicx}
\usepackage{appendix}
\usepackage[sectionbib]{chapterbib}
\usepackage{chapterbib}
\begin{document}
...
\include{background}
\include{ATRPcomp}
\include{CCTcomp}
\appendix
\include{AppCCT}
\end{document}
In each of my chapter sections and appendix I have:
\chapter{Compartmentalization in Catalytic Chain Transfer}
...
\bibliography{references}
Does the chapterbib also work for appendices or is there another option that could help?
I assume the package to work also within the appendix. I fact, when you issue the command \appendix the sectioning commands' behavior is the same, except for the numbering (and perhaps the heading).
In the main matter, the chapter command
\chapter{CCTcomp}
gives you
7 CCtcomp
(7 is an example). The commands sequence
\appendix
\chapter{AppCCT}
gives you
A AppCCT
The bibliography shoud work properly.

How to make 'appendix' appear in toc in Latex?

How to make word 'appendix' appear in the table of contents? Right now toc looks like this:
1 ......
2 ......
.
.
A .....
B .....
I would like it to be:
1 ......
2 ......
.
.
Appendix A .....
Appendix B .....
My latex source file structure is like this:
\begin{document}
\tableofcontents
\include{...}
\include{...}
\appendix
\include{...}
\include{...}
\end{document}
There's a couple of ways to solve this problem; unfortunately, I've only got a hack for you at this stage. One problem is that if we redefine the section number "A" to include the word "Appendix", it messes up the formatting of the table of contents. So instead, I've just defined a new sectioning command that prints the section without a number and inserts "Appendix X" manually.
Kind of ugly, but at least it works without having to change any markup :)
\documentclass{article}
\makeatletter
\newcommand\appendix#section[1]{%
\refstepcounter{section}%
\orig#section*{Appendix \#Alph\c#section: #1}%
\addcontentsline{toc}{section}{Appendix \#Alph\c#section: #1}%
}
\let\orig#section\section
\g#addto#macro\appendix{\let\section\appendix#section}
\makeatother
\begin{document}
\tableofcontents
\section{goo}
\label{a}
This is sec~\ref{a}
\section{har}
\label{b}
This is sec~\ref{b}
\appendix
\section{ji}
\label{c}
This is app~\ref{c}
\subsection{me}
does this look right?
\end{document}
For my thesis, I did the following:
\appendix
\addcontentsline{toc}{section}{Appendix~\ref{app:scripts}: Training Scripts}
\section*{Sample Training Scripts}
\label{app:scripts}
Blah blah appendix content blah blah blah.
Explanation: I manually added a line to the TOC so I would have "Appendix X:..." show up in my TOC. Then I excluded the actual section command from the TOC by using an asterisk.
This is probably most easily achieved by using the appendix
package, or the memoir class.
If you don't want to use a prepackaged solution, you'll have to
hack the sectioning commands. When I needed to do this for my
dissertation, I cloned the report class, and edited until I made
the margins lady happy. What you're looking for is the
definition of the \addcontentsline macro.
The appendix package is really good and simple solution. My answer can be helpful for who wants to change chapters numbering style, for example, with using cyrillic alphabet or roman digits. The appendices numbering style is hardcoded in the \#resets#pp command (I looked in sources here http://hal.in2p3.fr/docs/00/31/90/21/TEX/appendix.sty). I solved it by simple redefining this command to my own. Just add this code into your preamble:
\makeatletter
\renewcommand{\#resets#pp}{\par
\#ppsavesec
\stepcounter{#pps}
\setcounter{section}{0}
\if#chapter#pp
\setcounter{chapter}{0}
\renewcommand\#chapapp{\appendixname}
\gdef\thechapter{\Asbuk{chapter}} % changed
\else
\setcounter{subsection}{0}
\gdef\thechapter{\Asbuk{section}} % changed
\fi
\if#pphyper
\if#chapter#pp
\renewcommand{\theHchapter}{\theH#pps.\Asbuk{chapter}} % changed
\else
\renewcommand{\theHsection}{\theH#pps.\Asbuk{section}} % changed
\fi
\def\Hy#chapapp{\appendixname}%
\fi
\restoreapp
}
\makeatother
As a result,
Appendix A
Appendix B
Appendix C
...
will change to
Appendix A
Appendix Б
Appendix В
... etc
I'm not a latex expert, and I can't guarantee this code won't break something else.
Based on #Will Robertson's answer, the code below defines the same thing but for chapter and also fixes the fact that chapter* does not add to the header when using the fancyhdr package.
With this in the preable all issues are resolved.
\makeatletter
\newcommand\appendix#chapter[1]{%
\refstepcounter{chapter}%
\def\app#ct{Appendix \#Alph\c#chapter: #1}
\orig#chapter*{\app#ct}%
\markboth{\MakeUppercase{\app#ct}}{\MakeUppercase{\app#ct}}
\addcontentsline{toc}{chapter}{\app#ct}%
}
\let\orig#chapter\chapter
\g#addto#macro\appendix{\let\chapter\appendix#chapter}
\makeatother

Resources