[XeTeX] On an ugly hack to mathbf only in the local style.

Zdenek Wagner zdenek.wagner at gmail.com
Tue Oct 30 15:53:53 CET 2012

2012/10/30 Michaël Cadilhac <michael at cadilhac.name>:
> Hi there folks,
How about the following solution? I am writing it directly to the mail
without testing, I hope I won't make any error.

\def\vec#1{\do at vec#1_\do at vec}
\def\do at vec#1_#2\do at vec{\ifcat$#2$\mathbf{#1}\else\vec at subscript#1_#2\vec at subscript\fi}
\def\vec at subscript#1_#2_\vec at subscript{\mathbf{#1}_{#2}}

Macro \do at vec has two delimited parameters, the first ends with _, the
second ends with \do at vec. If the second parameter is empty, we output
just \mathbf{#1}, otherwise we output \mathbf{#1}_{#2}. Since we are
in math, the $ sign will never appear within the argument. The \ifcat
test compares the category codes of the following two tokens. If the
parameter is empty, then \ifcat$#2$ expands to \ifcat$$, the condition
is satisfied and thus \mathbf{#1} is displayed. If #2 is not empty,
\ifcat compares the category of $ with something else, the condition
is violated and thus the contents after \else is executed. We cannot
simply output \mathbf{#1}_{#2} here because #2 contains the underscore
appended in \vec when calling \do at vec. We therefore need another step
for consuming this underscore.

One might assume that it would be sufficient to use ...
\else\vec at subscript#1_#2\fi and use the following definition:

\def\vec at subscript#1_#2_{\mathbf{#1}_{#2}}

This willl often work but not alvays. Consider you use \vec{a_b_c}.
\do at vec will get #1=a, #2=b_c_
The simple version of \vec at subscript will always end at the nearest
underscore, ie #1=a #2=b
\vec{a_b_c} will then be expanded to \mathbf{a}_{b}c_
The first version consumes (I hope) everything and \vec{a_b_c} will
produce \mathbf{a}_{b_c}.

> I have gotten used to write vectors in my documents as $\vec{v}$.
> $\vec$, basically, is a mathbf.  Naturally, if I have vectors v_1,
> v_2, v_3, I'd like to write \vec{v_1}, \vec{v_2}, \vec{v_3}.  But
> here, I want to mathbf only the `v`.  Thus I used this ugly hack,
> contributed by fine people at StackExchange, which (1) saves the font
> in which a mathbf text would be displayed and (2) changes to mathbf
> ONLY the font of the current style:
> \def\vec#1{%
>   %% Gather all the fonts -- text, script, script script --.
>   \setbox0\hbox{$\mathbf{\xdef\vec at ts{\the\textfont\fam}%
>                          \xdef\vec at ss{\the\scriptfont\fam}%
>                          \xdef\vec at sss{\the\scriptscriptfont\fam}}$}%
>   \def\vec at font##1##2{##1 0=##2 ##1 1=##2}%
>   %% Return in the right style, apply the bf font ONLY for the local style.
>   \mathchoice{\hbox{$\displaystyle\vec at font{\textfont}{\vec at ts} #1$}}%
>         {\hbox{$\textstyle\vec at font{\textfont}{\vec at ts} #1$}}%
>         {\hbox{$\scriptstyle\vec at font{\scriptfont}{\vec at ss} #1$}}%
>         {\hbox{$\scriptscriptstyle\vec at font{\scriptscriptfont}{\vec at sss} #1$}}}%
> I do admit I have only the slightest clue of how this precisely works;
> especially, I'm not sure why I added \vec at font to set so many font
> families.  However, it did work, and I went with it.
> Then I switched to XeTeX --- I wanted to use the XITS fonts.  And as
> any ugly hack, mine wasn't supposed to be supported any longer; can't
> blame anyone but myself here.
> Thus I'm searching for some help to find a clean^H^H^H^H^Hworking
> solution for this problem.  Any help?
> Thanks a lot in advance!
> M.
> --------------------------------------------------
> Subscriptions, Archive, and List information, etc.:
>   http://tug.org/mailman/listinfo/xetex

Zdeněk Wagner

More information about the XeTeX mailing list