如何让我的书的附录按照改编书类中的“附录 A:附录标题”进行命名?

如何让我的书的附录按照改编书类中的“附录 A:附录标题”进行命名?
\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}

我已经调整了主要内容章节标题的外观,使其位于同一行,并且没有标签“章节”。但是,我希望附录有标签“附录”。MWE 无法实现这一点,我只能让 LaTeX 将标题打印两次。有什么想法吗?


编辑:代码现在应该可以编译了。一般来说,我使用 XeLaTeX 是因为 fontspec。

答案1

在此处输入图片描述

在此处输入图片描述

使用@Werner 的答案是否有“\if”命令可以确定命令是否已发出?允许\if进行按您的需要输出的测试:

\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}

要删除\frontmatter编号,请使用以下代码:

\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}

我不确定如何将其添加\appendixname到目录中(例如Appendix A. Appendix Title),使用包选项\usepackage[titletoc,title]{appendix}不会将其添加到目录中,并且可能值得提出一个新问题(链接回这个问题)。

相关内容