# 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.

-

Define an \antiqua command that checks whether the current font family is sans serif.

The example features a rather peculiar choice of fonts, which are just used for better showing the effect. So Termes is for normal text, Adventor for standard sans serif, Chorus is the replacement of Antiqua Sans and Cursor the replacement for Antiqua Serif. It's not meant to be an example of good typography. ;-)

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

\usepackage{pdftexcmds}

\usepackage{fontspec}
\setmainfont{TeX Gyre Termes}
\newfontfamily\antiquaserif{TeX Gyre Cursor}
\newfontfamily\antiquasans{TeX Gyre Chorus}

\makeatletter
\DeclareRobustCommand{\antiqua}[1]{{%
\ifnum\pdf@strcmp{\f@family}{\sfdefault}=\z@
\antiquasans
\else
\antiquaserif
\fi
#1%
}}
\makeatother

\begin{document}
\tableofcontents

So any appearance of the abbreviation \antiqua{NMR,} other abbreviations,
other paragraph text \antiqua{etc.} can be set perfectly here; also

\end{document}


-

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


i.e., you need to provide an optional argument which tells (Xe)LaTeX how the chapter header should be typeset in the Table of Contents.

Addendum From some of the follow-up comments I take it that you would like to fully automate this process, i.e., you would rather not to have to write the optional argument(s) of the various sectioning commands. I don't have a clear idea how this might be done, but I suspect that it would require some serious re-coding of the sectioning commands.

-
Thanks for the simple answer. Although I would also like an automatic solution, if there is one; since I'm not too keen on copying a (few) two-line-title(s) again just to add the one difference of tag (which occurs a few times in the long IUPAC names of chemicals) … ;) – Jan Jul 29 '12 at 21:03
@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

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

Almost three years later, revisiting the same problem I had before in a new document (PhD thesis as opposed to master's thesis, for those interested) and having a lot more time on my hands, I came up with a much nicer hack. I created a new command \antisanssubsection with the following definition:

\makeatletter
\def\antisans#1{{\antiquafont #1}}
\def\antisanssubsection#1{%
\section[#1]{%
\def\antisans##1{{\antiquasans ##1}}#1}}%
\makeatother


This included redefining \antisans{} as if it were \antiqua{}.

Typing:

\antisanssection{This is my Blackletter and \antisans{Antiqua} Section Title
Containing a Pointless \antisans{Lorem Ipsum}}


will now compile to:

\section[This is my Blackletter and {\antiquafont Antiqua} Section Title
Containing a Pointless {\antiquafont Lorem Ipsum}]{This is my Blackletter
and {\antiquafont Antiqua} Section Title Containing a Pointless {\antiquafont
Lorem Ipsum}}


(Line breaks included for clarity, not present in the original).

For some reason I did not manage to code the opposite (having \antisans{} defined the way it is, and switching its definition within the \section[ ] stuff, but that is part of a follow-up question.

-