2

I'd like to change the way appendix section headings appear with KOMA-Script. I can achieve the behaviour I want by manually patching the \begin{appendices}... with a \renewcommand{\sectionlinesformat}, but I'd like to use xpatch instead and handle it in my document's pramble. I know that my patch works because I am successfully changing a \cref alias with it. Under the %%%% ATTEMPTS SHOWN BELOW comment are two chunks of code I've tested that do not work. The errors are missing number treated as 0 and illegal unit of measurement for both attempts (without and with using the @hangfrom), and I additionally get something's wrong, perhaps missing an item with the @hangfrom.

\documentclass[12pt,
twoside=false,
letterpaper,
hidelinks,
parskip=half+,
%onehalfspacing,  % see setstrecj after \begin{document}
bibliography=numbered,
numbers=noenddot]{scrartcl}

\renewcommand\sectionlinesformat[4]{% used by free-standing headings with style=section
    \makebox[0pt][r]{#3}#4}

%\usepackage{appendix} % use this if you want Appendix in the TOC
\usepackage[titletoc,title]{appendix} % use this if you want Appendix in the TOC
\usepackage{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}  % cref
\usepackage{xpatch}

\crefname{appsec}{Appendix}{Appendices}

%%%% ATTEMPTS SHOWN BELOW

%\makeatletter
\xapptocmd\appendices{%
    \crefalias{section}{appsec}
    %%%% Test 1 does not work
%   \renewcommand{\sectionlinesformat}[4]{% change appendix format
%       \hskip #2#3\\#4%
%   }
    %%%% Test 2 does not work
%   \renewcommand{\sectionlinesformat}[4]{% change appendix format
%   \@hangfrom{\hskip #2#3}#4%
%   }
}{}{\PatchFailed}
%\makeatother




\begin{document}

\tableofcontents

\section{Some section with number in the margin}
Reference to \cref{appa}


\begin{appendices} 



\section{Something appended} \label{appa}
    %%% Test: this works
%%  \renewcommand{\sectionlinesformat}[4]{% change appendix format
%%      \hskip #2#3\\#4%
%%  }

Test appendix
\end{appendices}

\end{document}
1
  • The appendix isn't an environment, \appendix is a switch.
    – Keks Dose
    Sep 16, 2019 at 19:46

1 Answer 1

2

Use an alias:

\documentclass[12pt,
twoside=false,
letterpaper,
hidelinks,
parskip=half+,
%onehalfspacing,  % see setstrecj after \begin{document}
bibliography=numbered,
numbers=noenddot]{scrartcl}

\renewcommand\sectionlinesformat[4]{% used by free-standing headings with style=section
    \makebox[0pt][r]{#3}#4}

%\usepackage{appendix} % use this if you want Appendix in the TOC
\usepackage[titletoc,title]{appendix} % use this if you want Appendix in the TOC
\usepackage{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}  % cref
\usepackage{xpatch}

\crefname{appsec}{Appendix}{Appendices}

\xapptocmd\appendices{%
    \crefalias{section}{appsec}%
    \let\sectionlinesformat\modifiedsectionlinesformat
}{}{}
\makeatletter
\newcommand{\modifiedsectionlinesformat}[4]{% change appendix format
  \@hangfrom{\hskip #2#3}#4%
}
\makeatother

\begin{document}

\tableofcontents

\section{Some section with number in the margin}
Reference to \cref{appa}

\begin{appendices} 

\section{Something appended} \label{appa}

Test appendix
\end{appendices}

\end{document}

enter image description here

2
  • It worked great, thank you. But why do I need an alias for it to work? Sep 16, 2019 at 20:39
  • 1
    @likethevegetable Managing # in the argument of \xpatchcmd and similar commands is always a pain. It can be done, but the less you put there, the better.
    – egreg
    Sep 16, 2019 at 20:44

You must log in to answer this question.

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