Text Formatting" character lost special meaning within a variable

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Fibonacci
Posts: 37
Joined: Tue Jan 25, 2011 1:16 am

" character lost special meaning within a variable

Post by Fibonacci »

Hello.

The best way to explain this is with the MWE:

Code: Select all

\documentclass{article}
\usepackage[spanish]{babel}
\def\algo{2"o}
\begin{document}
2"o\algo
\end{document}
The output, instead of being the string 2.º twice, looks like 2.º2”o. Moving the definition to a later point in the document works fine:

Code: Select all

\documentclass{article}
\usepackage[spanish]{babel}
\begin{document}
\def\algo{2"o}
2"o\algo
\end{document}
However, I absolutely need that definition in the preamble. Is there any way to get the special effect of the " character in the preamble?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

" character lost special meaning within a variable

Post by rais »

Hi there,
the babel package delays the activation of its special characters, the so-called shorthands, to the begin of the document not to confuse other packages, IIRC.
To activate one of them in the preamble, you can use \shorthandon/\shorthandoff, i.e.,

Code: Select all

\documentclass{article}
\usepackage[spanish]{babel}
\shorthandon{"}
  \def\algo{2"o}
\shorthandoff{"}
\begin{document}
2"o\algo
\end{document}
KR
Rainer
Fibonacci
Posts: 37
Joined: Tue Jan 25, 2011 1:16 am

Re: " character lost special meaning within a variable

Post by Fibonacci »

Is there a way to enable all shorthands at once?
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

" character lost special meaning within a variable

Post by rais »

well, the \shorthandon/\shorthandoff macros aren't limited to single characters.
Perhaps babel's option `KeepShorthandsActive' does what you want, too.

KR
Rainer
Fibonacci
Posts: 37
Joined: Tue Jan 25, 2011 1:16 am

Re: " character lost special meaning within a variable

Post by Fibonacci »

Thank you, will keep looking then.
Post Reply