[texhax] Auto capitilise first letter of acronym

Viktor Ahlqvist viktor at datakaka.net
Sat Apr 28 18:27:49 CEST 2012


Hello TeXhax

I'm trying to write a macro that auto capitalise the first letter in
an acronym if the acronym is the first word in a sentence. So far I
have come up with the following macro.

\sfcode`\.=1001\sfcode`\?=1001\sfcode`\!=1001\sfcode`\:=1001
\newcommand{\acr}[1]{\ifnum\spacefactor=1001
\MakeUppercase{\protect\StrLeft{#1}{1}}\textsc{\StrGobbleLeft{#1}{1}}\else
\textsc{#1}\fi}
It uses \StrLeft and \StrGobbleLeft from the xstring package to make
the first letter uppercase and the rest small caps.

MWE:
\documentclass{article}
\usepackage{xstring}
\sfcode`\.=1001\sfcode`\?=1001\sfcode`\!=1001\sfcode`\:=1001
\newcommand{\acr}[1]{\ifnum\spacefactor=1001
\MakeUppercase{\protect\StrLeft{#1}{1}}\textsc{\StrGobbleLeft{#1}{1}}\else
\textsc{#1}\fi}
\begin{document}
Hello. \acr{gps} is a \acr{gnss}. \acr{glonass} is another \acr{gnss}
\end{document}

The MWE works as expected, the output is "Hello. Gps is a gnss.
Glonass is another gnss" (with some small caps)

I have two problems however, that I can't find a solution for.
1) I get an error if I put \acr{} in the beginning of a paragraph.
! Improper \spacefactor.
\acr #1->\ifnum \spacefactor
                             =1001 \MakeUppercase {\protect \StrLeft {#1}{1}...

2) The macro fails to recognise the beginning of sentences if I use
polyglossia and set the default language to Swedish. Polyglossia and
English works fine. I think this has something to do with
frenchspacing, since the only difference I can find between the two
languages in \PolyglossiaSetup (in polyglossia.dtx) is
"frenchspacing=true" in Swedish. If I enable frenchspacing with the
\frenchspacing command (and omit polyglossia), everything works just
fine.

Does someone know how to fix the above problems?

/Viktor Ahlqvist


More information about the texhax mailing list