0
\documentclass{book}
\usepackage{fontspec}
\usepackage{appendix}
\usepackage{kantlipsum}

\newfontfamily\chapterfont{Verdana}

\makeatletter % https://tex.stackexchange.com/questions/18604/chapter-formatting
\def\@makechapterhead#1{%
  {\parindent \z@ \raggedright \chapterfont\fontsize{14pt}{0pt}\bfseries
    \ifnum \c@secnumdepth >\m@ne
        \if@mainmatter % Check if we are in the main matter
            \thechapter.\ % <-- Chapter # (without "Chapter")
        %\if@appendix
            \appendixname~\thechapter:% <-- "Appendix A:"
        \fi
    \fi
    \interlinepenalty\@M
    #1\par\nobreak% <------------------ Chapter title
    \vskip 12pt% <------------------ Space between chapter title and first paragraph
  }}
\makeatother

\begin{document}
  \frontmatter
    \chapter{Preface}
    \kant[10]
    \tableofcontents
  \mainmatter
    \chapter{This is a chapter}
    \kant[1]
    \chapter{Second chapter}
    \kant
  \appendix
    \chapter{Appendix title}
    \kant[6]
\end{document}

I have adapted the appearance of my main matter chapter titles to be on the same line and without the label "Chapter". However, I would like for my appendices to have the label Appendix. The MWE doesn't achieve this, I've just managed to get LaTeX to print the title twice. Any thougths?


Edit: code should compile now. Generally, I use XeLaTeX because of fontspec.

BlueIris
  • 339
  • Are you compiling with lualatex or xelatex? Probably won't make much of a difference in this case but is always helpful to include which you are compiling with if it is not pdflatex, there are situations where they are very different – JamesT May 23 '23 at 09:27
  • Your code doesn't compile either on my TeX Live 23 nor on Overleaf lualatex 22, can you make your example compile please? – JamesT May 23 '23 at 09:29

1 Answers1

0

enter image description here

enter image description here

Using the answer by @Werner to Is there an "\if" command that determines if a command has been issued? allows an \if test which outputs as you desire:

\documentclass{book}
\usepackage{fontspec}
\usepackage{appendix}
\usepackage{kantlipsum}

\newfontfamily\chapterfont{Verdana}

\makeatletter % https://tex.stackexchange.com/a/233251 \newcommand{\inappendix}{TT\fi\expandafter\ifx@chapapp\appendixname}

% https://tex.stackexchange.com/questions/18604 \def@makechapterhead#1{% {\parindent \z@ \raggedright \chapterfont\fontsize{14pt}{0pt}\bfseries \ifnum \c@secnumdepth >\m@ne \if\inappendix \appendixname~\thechapter:\ % <-- "Appendix A:" \else \thechapter.\ % <-- Chapter # (without "Chapter") \fi \fi \interlinepenalty@M #1\par\nobreak% <------------------ Chapter title \vskip 12pt% <------------------ Space between chapter title and first paragraph }} \makeatother

\begin{document} \mainmatter \chapter{This is a chapter} \kant[1] \chapter{Second chapter} \kant \appendix \chapter{Appendix title} \kant[6] \end{document}


To remove the \frontmatter numbering then use the following code:

\documentclass[oneside]{book}

\usepackage{fontspec} \usepackage{appendix} \usepackage{kantlipsum}

\newfontfamily\chapterfont{Verdana}

\makeatletter % https://tex.stackexchange.com/a/233251 \newcommand{\inappendix}{TT\fi\expandafter\ifx@chapapp\appendixname}

% https://tex.stackexchange.com/questions/18604 \def@makechapterhead#1{% {\parindent \z@ \raggedright \chapterfont\fontsize{14pt}{0pt}\bfseries \ifnum \c@secnumdepth >\m@ne \if\inappendix \appendixname~\thechapter:\ % <-- "Appendix A:" \else \if@mainmatter \thechapter.\ % <-- Chapter # (without "Chapter") \fi \fi \fi \interlinepenalty@M #1\par\nobreak% <------------------ Chapter title \vskip 12pt% <------------------ Space between chapter title and first paragraph }} \makeatother

\begin{document} \frontmatter \chapter{Preface} \kant[10] \tableofcontents \mainmatter \chapter{This is a chapter} \kant[1] \chapter{Second chapter} \kant \appendix \chapter{Appendix title} \kant[6] \end{document}

I am not sure how to add the \appendixname to the TOC (e.g. Appendix A. Appendix Title), using the package options \usepackage[titletoc,title]{appendix} doesn't add it to the TOC and is likely worth a new question (linked back to this one).

JamesT
  • 3,169
  • Yeah! Although, now \frontmatter chapters have "0. Chapter title" as a number, which wasn't the case earlier (updated MWE to include front matter). Also the "Appendix A: "-part is not included in the TOC, why not? How do I change that? – BlueIris May 23 '23 at 11:56
  • 1
    @BlueIris I don't mind answering the updated MWE and I am working on it now but in the future please ensure your question includes these cases, modifying a question after it has already been answered to something different isn't good for either of us - I need to update the answer and you need to wait longer. Will edit the answer soon as it should be simple :) – JamesT May 23 '23 at 12:05