5

I would like a set of macros to switch the font alternative in ConTeXt, to either italics, slanted, or bold, and I would like them to combine in an intuitive manner, similarly to LaTeX's \textit and \textbf.

The ConTeXt wiki says that the commands \typeface, \boldface, and \swapface behave similarly to how I want my macros to behave. Unfortunately, \boldface does not always preserve italics or slant, and there are no analogous commands for italics and slanted font alternatives. The following image, from here, demonstrates the behavior of the mentioned commands for various current font alternatives.

demonstration of commands
(source: contextgarden.net)

After looking at the ConTeXt source code for \typeface, \boldface, and \swapface, found here, I tried creating my desired macros with the following code:

\unexpanded\def\normalItalic
    {\relax
        \ifx\fontalternative\s!bf \bi \else
        \ifx\fontalternative\s!bi \bi \else
        \ifx\fontalternative\s!bs \bi \else
                                  \it \fi\fi\fi}
\unexpanded\def\normalSlanted
    {\relax
        \ifx\fontalternative\s!bf \bs \else
        \ifx\fontalternative\s!bi \bs \else
        \ifx\fontalternative\s!bs \bs \else
                                  \sl \fi\fi\fi}
\unexpanded\def\normalBold
    {\relax
        \ifx\fontalternative\s!it \bi \else
        \ifx\fontalternative\s!sl \bs \else
        \ifx\fontalternative\s!bi \bi \else
        \ifx\fontalternative\s!bs \bs \else
                                  \bf \fi\fi\fi\fi}
\def\Italic#1{{\normalItalic #1}}
\def\Slanted#1{{\normalSlanted #1}}
\def\Bold#1{{\normalBold #1}}

Unfortunately, these do not combine as I would expect, based on the behavior of \typeface, \boldface, and \swapface. \Italic{Hello \Bold{World}!} produces the output Hello World! instead of the expected Hello World!

What causes my code to behave differently from the macros \typeface, \boldface, and \swapface so that they do not combine correctly?

2

As explained here on the ConTeXt wiki, some characters @, !, and ? are not normally considered letters for the purpose of commands. In order to use these characters in a command, it must be surrounded by \unprotect and \protect.

It appears that \s!bf and similar commands are defined to be what can be returned by the macro \fontalternative, so they are better to use than just bf or similar in case \fontalternative is ever changed. Since macros like \s!bf need to be used in the attempted solution, \unprotect must be placed before the first definition, and \protect must be placed after the last definition. Without \unprotect, it seems \s!bf is interpreted as \s! bf so the \ifx ladder always follows the \else path.

With \unprotect and \protect added, \Italic{Hello \Bold{World}!} produces the expected output: Hello World!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.