# Using xpatch to alter \sectionlinesformat to change format for appendices headings with KOMA-Script

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,
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{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}

• The appendix isn't an environment, \appendix is a switch. – Keks Dose Sep 16 '19 at 19:46

Use an alias:

\documentclass[12pt,
twoside=false,
letterpaper,
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{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}


• It worked great, thank you. But why do I need an alias for it to work? – likethevegetable Sep 16 '19 at 20:39
• @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 '19 at 20:44