# KOMA-Script throws “! Extra \else” error

With a minimal document such as

\documentclass{scrartcl}
\begin{document}
\section{Introduction}
\end{document}


I am getting the following error with latex filename.tex

! Extra \else.
\@sect ...sname }{\scr@ds@tocentry }\fi \fi \else
\def \@svsechd {#6{\hskip ...
l.49 \section{Introduction}


Can anyone help pinpoint where the problem might be?

• If I change the document class to be article, it works fine
• If I remove the \pagestyle{headings}, it works fine
• If I change headings to empty, it works fine

Versions:

pdfTeX 3.1415926-2.5-1.40.14 (TeX Live 2013/Arch Linux)
kpathsea version 6.1.1
...
Compiled with libpng 1.6.10; using libpng 1.6.10
Compiled with zlib 1.2.8; using zlib 1.2.8
Compiled with poppler version 0.24.5


and KOMA-Script (scrartcl.cls)

%%% From File: $Id: scrkernel-version.dtx 1560 2013-12-19 07:13:30Z mjk$

-

As Speravir's answer points out, this is a bug; the headings page style should work with KOMA classes. A possible work-around is to load the scrpage2 package and change to useheadings style:

\documentclass{scrartcl}
\usepackage{scrpage2}
\begin{document}
\section{Introduction}
\end{document}


Page 206 of the scrguien document mentions this:

The package scrpage2 is meant primarily for use of the supplied styles or for defining one’s own styles. However, it may be necessary to shift back also to a style provided by the docu- ment class. It might appear that this should be done with \pagestyle{headings}, but this has the disadvantage that commands \automark and \manualmark, to be discussed shortly, do not function as expected. For this reason one should shift back to the original styles using v\pagestyle{useheadings}, which chooses the correct page styles automatically for both manual and automatic running headings.

-
@Speravir Thank you. I agree this answer shouldn't be deleted but also that it should be edited to make clear that the use of headings should be perfectly appropriate but that it doesn't work due to a bug. Would either of you care to comment on the code in my answer? This seems to work but I don't know if it is the right way to fix it or not. Unfortunately, I don't read German either... –  cfr Mar 21 at 0:37
@cfr you're answer seems to be right, but I cannot say for sure; I am not familar with the "KOMA way" of doing things. –  Gonzalo Medina Mar 21 at 0:41
That's my worry, too. I don't use these classes myself. I really just followed the error and compared with the other places using the same macro. (e.g. \MakeMarkcase for section and subsection below.) This is why I expanded the MWE a bit. (Actually, I added a bunch more but didn't want to post the entire thing as it seemed to work.) But that is hardly rigorous testing! –  cfr Mar 21 at 0:43
Excellent stuff guys! Thanks everyone for the detailed responses: a workaround and a hack. You guys rock –  Mack Mar 21 at 1:13
@Mack: Then apart from accepting Gonzalo’s answer give an upvote to all, too. ;-) –  Speravir Mar 21 at 1:20

Just as addition to Gonzalo’s answer. You’ve run into a known bug of KOMA-Script version 3.12.

Cf. (alas only in German) Bekannte Probleme und Änderungen in KOMA-Script 3.12:

scrartcl:
[…]
Wenn man den Seitenstil headings aktiviert, wird man mit Fehlern wegen eines \fi zu viel
Workaround: Paket scrlayer-scrpage (oder notfalls scrpage2) laden und Seitenstil
Hinweis: Der Fehler ist in der experimentellen Release behoben.


It means that the error you observed is removed in an experimental version you can download from a page, which is linked behind the words “experimentellen Release”, but is again in German. Fortunately Markus Kohm created for this page an English companion: Using most current KOMA-Script with TeX Live, MacTeX or MiKTeX.

Without using this experimental version you should load the (KOMA-Script) package scrlayer-scrpage and use the pagestyle scrheadings, similar like Gonzalo suggested. (In my eyes with version 3.12 there is no need anymore for using package scrpage2, because scrlayer-scrpage is downwards compatible, as long as you never have used internal commands of scrpage2.)

-

The use of headings produces the following error:

! Extra \else.
\@sect ...sname }{\scr@ds@tocentry }\fi \fi \else
\def \@svsechd {#6{\hskip ...
l.6 \section{Introduction}

?


As explained in Speravir's answer, this is a bug in the current (non-experimental) version of the class. If you prefer to use the standard headings in preference to the workaround in Gonzalo Medina's answer, you might like to try the following fix.

# Fixing the definition of headings

The following redefinition appears to solve the problem. The line marked MODIFIED contains an extra \fi in the original which is not present in other uses of \MakeMarkcase. The fix modifies that line to remove the \fi in line with other uses of the macro.

\documentclass{scrartcl}
\usepackage{kantlipsum}
\makeatletter
\set@tempdima@hw\hss\hb@xt@ \@tempdima{\vbox{%
\if@hsl \vskip 1.5\p@ \hrule \fi}}}%
\set@tempdima@hw\hb@xt@ \@tempdima{\vbox{%
\if@twoside\else\hfil\fi}}%
\if@hsl \vskip 1.5\p@ \hrule \fi}}\hss}%
\renewcommand*{\@evenfoot}{%
\set@tempdima@hw\hss\hb@xt@ \@tempdima{\vbox{%
\if@fsl \hrule \vskip 3\p@ \fi
\hb@xt@ \@tempdima{{\pagemark\hfil}}}}}%
\renewcommand*{\@oddfoot}{%
\set@tempdima@hw\hb@xt@ \@tempdima{\vbox{%
\if@fsl \hrule \vskip 3\p@ \fi
\hb@xt@ \@tempdima{{\hfil\pagemark
\if@twoside\else\hfil\fi}}}}\hss}%
\renewcommand*{\sectionmark}[1]{%
\if@twoside\expandafter\markboth\else\expandafter\markright\fi
{\MakeMarkcase{\ifnumbered{section}{\sectionmarkformat}{}##1}}{}}% MODIFIED
\renewcommand*{\subsectionmark}[1]{%
\if@twoside
\markright{%
\MakeMarkcase{\ifnumbered{subsection}{\subsectionmarkformat}{}##1}%
}%
\fi
}%
}%
\makeatother

\begin{document}
\section{Introduction}
\kant[1]
\subsection{Subsection}
\kant[2]
\section{Another Section}
\kant[3]
\section{Yet Another Section}
\kant[4]
\subsection{Another Subsection}
\kant[5]
\end{document}


# The result

-
I’m not that good to decide, whether you made something “wrong” (obviously it is a working patch at least for your example code). But if you’re lazy you could download the experimental resource and compare how Markus Kohm fixed it himself (but note, there are other fixed bugs, too). –  Speravir Mar 21 at 1:22

As has already been explained this is a bug of KOMA 3.12 which is known to the developers since late December 2013 and fixed in an experimental release. As I encountered myself the bug early January, I had to patch it for my own sources and here is my patch:

\pagestyle{headings}% must be before the patch
\makeatletter
\def\buggysectionmark #1{% KOMA 3.12 as released to CTAN December 2013
\if@twoside\expandafter\markboth\else\expandafter\markright\fi
{\MakeMarkcase{\ifnumbered{section}{\sectionmarkformat\fi}{}#1}}{}}
\ifx\buggysectionmark\sectionmark
\def\sectionmark #1{%
\if@twoside\expandafter\markboth\else\expandafter\markright\fi
{\MakeMarkcase{\ifnumbered{section}{\sectionmarkformat}{}#1}}{}}
\fi
\makeatother


The idea is that once your installation has the next release of KOMAscript where this problem will have been fixed, the patch will do nothing (because the \ifx test will detect that \sectionmark` is not the buggy one anymore) and the patch will do nothing you don't have to fiddle again with your legacy sources.

-