# Switching fonts in section titles while maintaining a serif/sans-serif distinction for ToC vs. title itself

The solution to this problem is probably something absurdly simple, but I'm too stupid to see it …

Consider a document written in blackletter. German blackletter typography rules require latin words and uppercase abbreviations to be set in antiqua. I have followed the advice of the fontspec package and created a new font family and a little \antiqua-macro. This works perfectly inside text paragraphs, because I know if I want a serif or a sans-serif typeface. However, I am not able to create something, that would render the antiqua part in sans-serif in the section title itself, while turning the same thing to the corresponding serif typeface in the table of contents … What's the simple solution?

\documentclass[12pt,a4paper,twoside]{scrreprt}
{numerous other packages}
\setmainfont[Mapping=tex-text]{UnifrakturMaguntia} % for blackletter
\newfontfamily\antiquafont[Mapping=tex-text]{LiberationSerif} %antiqua serif typeface
\newfontfamily\antiquasans[Mapping=tex-text]{LiberationSans} %antiqua sans-serif typeface
\newcommand\antiqua[1]{{\antiquafont #1}} %creates my antiqua-macro
\newcommand\antisans[1]{{\antiquasans #1}} %creates a similar macro for sans-serif antiqua typing

\begin{document}
\tableofcontents
So any appearance of the abbreviation \antiqua{NMR,} other abbreviations,
other paragraph text \antiqua{etc.} can be set perfectly here, but
it mustn't.
\end{document}


If I'm perfectly honest I wouldn't have expected this code to work without flaws, because there is no connection between the serif and the sans-serif part (save the fact that the name starts with the same word). The minimal example can of course be worked with any other combination of two fonts different enough to tell them apart.

-

A possible solution would be to define a switch that defines whether the command appears in the main text or inside a section heading, and that is called automatically by section headings

\documentclass[12pt,a4paper,twoside]{scrreprt}

\usepackage{etoolbox}

\setmainfont[Mapping=tex-text]{Times} % for blackletter
\newfontfamily\antiquafont[Mapping=tex-text]{Optima} %antiqua serif typeface
\newfontfamily\antiquasans[Mapping=tex-text]{Courier} %antiqua sans-serif typeface
\newcommand\antiqua[1]{{\antiquafont #1}} %creates my antiqua-macro
\newcommand\antisans[1]{{\iftoggle{insection}{\antiquasans #1}{\antiquafont #1}}} %creates a similar macro for sans-serif antiqua typing

\newtoggle{insection}

\makeatletter
\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{\chapterpagestyle}%
\global\@topnum\z@
\@afterindentfalse
\toggletrue{insection}
\secdef\@chapter\@schapter
}
\renewcommand\section{\@startsection{section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\toggletrue{insection}\ifnum \scr@compatibility>\@nameuse{scr@v@2.96}\relax
\setlength{\parfillskip}{\z@ plus 1fil}\fi
\raggedsection\normalfont\sectfont\nobreak\size@section}%
}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
{\toggletrue{insection}\ifnum \scr@compatibility>\@nameuse{scr@v@2.96}\relax
\setlength{\parfillskip}{\z@ plus 1fil}\fi
\raggedsection\normalfont\sectfont\nobreak\size@subsection
}%
}
\makeatother

\begin{document}
\tableofcontents
So any appearance of the abbreviation \antiqua{NMR,} \textsf{NMR} other
abbreviations,other paragraph text \antiqua{etc.} can be set perfectly here,
it mustn't.

\chapter{Test \antisans{Test}}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\end{document}


Caveat: One has to redefine all section commands (e.g., part, section, subsections ...) and insert the switch in the right place.

-
Thanks for this answer. I thought I might add that there are no chapters in my actual document that would require this, only sections and subsections. Just copying the chapter-commands and replacing 'section' where it says 'chapter' doesn't do the job unfortunately. –  Jan Jul 30 '12 at 8:23

I think that what you need to do is to augment the instruction

\chapter{My Blackletter Text about \antisans{NMR}}


to

\chapter[My Blackletter Text about \antiqua{NMR}]{My

@Jan - Without access to the fonts to actually execute your MWE, I'm afraid I'm in a position to work out all the programming. Maybe somebody else will come up with an extended solution. Do note that I've managed to simplify the suggested solution (by no longer requiring a \protect instruction). Just how many sectioning headers does your document have that might require attention of the nature implied by your question? –  Mico Jul 29 '12 at 21:21
UnifrakturMaguntia can be downloaded [here] (unifraktur.sourceforge.net/maguntia.html), UnifrakturCook is not far down the way. Liberation is standard on Linux environments, so it's probably downloadable somewhere only I don't know where. I don't see any reason though, why it should not work with any combination of two fonts? It's 17 headings, of which three contain quite a few \antisans-tags. –  Jan Jul 29 '12 at 21:35
@Jan - thanks for providing the links; I've installed the fonts and was able to verify that my suggested solution indeed does the job. :-) However, as I note in an addendum to the answer, I can't see a way of making your objective of fully automating the process, at least not without some major rewriting of all sectioning commands. In the end, it may be less effort for you to provide the optional arguments of the sectioning commands by hand. This shouldn't be so hard to do anyway, right: You copy the argument of the sectioning command, paste it into [], and then do a search-and-replace. –  Mico Jul 30 '12 at 0:49