2

Background and Problem

I'm using XeLaTeX to typeset a document in blackletter font, which requires the use of antiqua fonts for loan words and others. So I have created two commands to explicitly set phrases in antiqua: \antiqua{}, which uses a serif variant, and \antisans{}, which uses a sans-serif variant. I ran into problems when using \antisans{} in (sub)section titles (which is what this question is about) and finally got around it with the code I posted there. (See also MWE) (All attempts to code this using \renewcommand and the likes failed)

MWE

\documentclass[12pt,a4paper,twoside]{scrreprt}
{numerous other packages}
\usepackage{mathspec} %loads fontspec
\setmainfont[Mapping=tex-text]{UnifrakturMaguntia} % for blackletter
\setsansfont[Mapping=tex-text]{UnifrakturCook} % for headers,
\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]{{\antiquafont #1}} %creates a similar macro for
% use in titles.

\makeatletter%
\def\antisanssubsection#1{%    Defines the new subsection command
\section[#1]{%                 inserts a version with serifs into the short title
\def\antisans##1{{\antiquafont ##1}}#1}}% redefines \antisans{} for use within the
%                                         actual title.
\makeatother

\begin{document}
\tableofcontents
\antisanssubsection{A Pointless Title \antisans{Lorem Ipsum} Title Containing \antisans{NMR}}
This version of the code works exactly as it should with section titles, the table
of contents, \antiqua{etc.} I am unhappy about the `wrong' perceived definition of
the \antiqua{antisans} macro, though, and wish I could get around the limitation.
\end{document}

Non-Working Example and Question

This above example works fine. However, as I wrote, I am unhappy about the 'wrong' definition of the \antisans{} macro, which should create a sans-serif text according to its name. I don't know yet, but I might be tempted to use it outside of section titles when labelling images and the like. Which is why my starting point was the following non-working central coding section:

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

It reported:

Illegal parameter number in definition of \scr@ds@tocentry.
<to be read again>
                   1

followed by the first line of text which contained an \antisanssubsection

My understanding is that TeX is confused by the appearance of ##1 (twice) within the square brackets of the \section command of the macro. I don't understand why, though.

Is there a way to code this so that it works in the intended way?

To rephrase the question:

My code is redefining a macro in the {} part of a (sub)section, aka the section title. If I switch the order to redefine the same macro (in a different way) in the [] part (the short title), it fails to compile and I get an error.

Why do I get the error, and how can I rewrite the code so that the macro can be redefined inside the square brackets?

Jan
  • 829

1 Answers1

3

Compare the current font family: if it is sans serif, use \antiquasans, otherwise use \antiquaserif.

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

\usepackage{pdftexcmds}

\usepackage{fontspec} %loads fontspec
\setmainfont{Old Standard} % for blackletter
\setsansfont{Futura} % for headers,
\newfontfamily\antiquaserif{LiberationSerif} %antiqua serif typeface
\newfontfamily\antiquasans{LiberationSans} %antiqua sans-serif typeface


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

\begin{document}
\tableofcontents

\subsection{A Pointless Title \antiqua{Lorem Ipsum} Title Containing \antiqua{NMR}}
This version of the code works exactly as it should with section titles, the table
of contents, \antiqua{etc.} I am unhappy about the `wrong' perceived definition of
the \antiqua{antisans} macro, though, and wish I could get around the limitation.
\end{document}

enter image description here

egreg
  • 1,121,712