2012/10/30 Michaël Cadilhac <[email protected]>:
> 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@vec#1_\do@vec}
\def\do@vec#1_#2\do@vec{\ifcat$#2$\mathbf{#1}\else\vec@subscript#1_#2\vec@subscript\fi}
\def\vec@subscript#1_#2_\vec@subscript{\mathbf{#1}_{#2}}
Explanation:
Macro \do@vec has two delimited parameters, the first ends with _, the
second ends with \do@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@vec. We therefore need another step
for consuming this underscore.
One might assume that it would be sufficient to use ...
\else\vec@subscript#1_#2\fi and use the following definition:
\def\vec@subscript#1_#2_{\mathbf{#1}_{#2}}
This willl often work but not alvays. Consider you use \vec{a_b_c}.
\do@vec will get #1=a, #2=b_c_
The simple version of \vec@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@ts{\the\textfont\fam}%
> \xdef\vec@ss{\the\scriptfont\fam}%
> \xdef\vec@sss{\the\scriptscriptfont\fam}}$}%
> \def\vec@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@font{\textfont}{\vec@ts} #1$}}%
> {\hbox{$\textstyle\vec@font{\textfont}{\vec@ts} #1$}}%
> {\hbox{$\scriptstyle\vec@font{\scriptfont}{\vec@ss} #1$}}%
> {\hbox{$\scriptscriptstyle\vec@font{\scriptscriptfont}{\vec@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@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
http://hroch486.icpf.cas.cz/wagner/
http://icebearsoft.euweb.cz
--------------------------------------------------
Subscriptions, Archive, and List information, etc.:
http://tug.org/mailman/listinfo/xetex