I'm using scrartcl and am trying to move the appendices in the toc one level up so that they are formatted as if they where a subsection instead of a section.
I've adapted my solution from this question and my current MWE (min.tex) looks like this:
\documentclass{scrartcl}
\usepackage[page]{appendix} % better appendices: \begin{appendices}
\usepackage{etoolbox} % needed for code below (\AtBeginEnvironment)
\AtBeginEnvironment{appendices}{%
\addappheadtotoc % add Appendices to toc
\makeatletter
\addtocontents{toc}{% % move appendix sections one level up in toc
\begingroup % make sure we reset section level redefinition after appendices
\let\protect\l@section\protect\l@subsection
\let\protect\l@subsection\protect\l@subsubsection}}
\AtEndEnvironment{appendices}{%
\addtocontents{toc}{\endgroup}} % reset section level redefinition
\begin{document}
\tableofcontents
\begin{appendices}
\section{Variablen}
\section{Tabellen}
\end{appendices}
\end{document}
When compiling with lualatex the second time to get the table of contents, I get the following error:
) (./min.toc
! LaTeX Error: Something's wrong--perhaps a missing \item.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.3 ...tsline {section}{\numberline {A}Variablen}{1}
Looking at min.toc, it is clear why. There are spourious spaces added in the commands
\begingroup \let \l @section\l @subsection \let \l @subsection\l @subsubsection
which should read
\begingroup \let \l@section\l@subsection \let \l@subsection\l@subsubsection
When I add the \let commands just after \begin{appendices} it works fine. So my suspicion is, that \AtBeginEnvironment is adding those spaces. I'd rather like to keep this in the preamble and not put it in the main document. Is there a way to fix this?
Update
The formatting problem described below is fixed imho quite more elegantly in the accepted answer than my edit here. I suggest to use that. But I'll leave my fix for the record.
The solution from the answer below and from the above question with the fix in the comments by cgnieder has inconsistency problems with the "Appendices" heading of the appendix package. To adapt it to the formatting of sections used in scrartcl, the command \@sec@pppage has to be renwed (modified from the appendix package):
\makeatletter
\renewcommand{\@sec@pppage}{%
\par
\addvspace{4ex}%
\@afterindentfalse
{\parindent \z@ \raggedright
\interlinepenalty \@M
% the following line was changed from the original
\usekomafont{disposition} \usekomafont{section} \appendixpagename%
\markboth{}{}\par}%
\if@dotoc@pp
\addappheadtotoc
\fi
\nobreak
\vskip 3ex
\@afterheading
}
\makeatother
This code might be a bit overkill, but it works. Unfortunately, my LaTeX programming skills are not quite where they could be. Maybe someone has a better solution?

\begingroupinside an argument without\endgroupdoes not look very healthy. And you mean 'pushing' down the appendix, since forscrartclappendix entries are sections, subsections is just one level below – Aug 21 '14 at 14:19\makeatletterbefore\AtBeginEnvironmentand a\makeatotherafter all the patching is done – cgnieder Aug 21 '14 at 14:23\AtEndEnvironment. If I don't do it at the end of theappendicesenvironment, the bibliography or any other section that comes after is also pushed down. – justibus Aug 21 '14 at 14:30