# Using simple quotes “” instead of \text{}

Is there a way to use newcommand or some other nice technique to transform any occurence of

"bla"


in math mode into

\text{bla}


The reason I ask is because it is easy, and doesn't interrupt the flow of reading the (La)TeX code as much.

I would like to keep Xe(La)TeX's nice quote behavior (ie it can use the regular "" instead of backticks to quote some text outside math mode).

-
It might be more desirable to use some auto-complete / auto-expansion in your editor of choice, as opposed to the (albeit interesting) solution of @Martin –  Pieter Dec 12 '11 at 12:59
@Pieter: I'm trying to remove the obstructive \text{} commands to improve general readability of the tex code. –  rubenvb Dec 12 '11 at 13:01

I don't think that marking code this way adds to its readability. Nevertheless, you can change the behavior of " in math mode with the following TeX magic:

\documentclass{article}
\usepackage{amsmath}
\begingroup\lccode~="
\lowercase{\endgroup\def~}#1"{\text{#1}}
\mathcode"="8000

\begin{document}
"Abc" $f_"abc"$
\end{document}


However, keep in mind that " is not the “right” way to type a double quote, and it's preferable to use the combinations  and ''.

If the quotes your text editor inserts are “ and ”, then the following should work:

\begingroup\lccode~=“
\lowercase{\endgroup\def~}#1”{\text{#1}}
\mathcode“="8000


But definitely not when using pdflatex and UTF-8 encoding. XeLaTeX and LuaLaTeX are OK. Again, I don't recommend such a type of markup, which is prone to errors and less readable than an explicit \text{...} markup.

Let's see how it works; the trick

\begingroup\lccode~=“
\lowercase{\endgroup\def~}#1”{\text{#1}}


is very common (it's in the TeXbook) in order to give a meaning to the active version of a character (in this case “) as \lowercase does its job and puts the converted token list back in the input stream, so that TeX will see the lowercase version of ~ (still active as is ~ in the default setting). Thus TeX will swallow

\def“#1”{\text{#1}}


The macro “ is defined to look for the following ” and consider anything in between as its argument, which is passed to \text.

With \mathcode“="8000 we are telling (Xe)TeX that “ in math mode must be converted to its active version. In text mode the nature of “ is not changed.

Why does the first version, with ", work? Because " is not active, its category code is still 12 and the definition tells TeX to look for the next category 12 " that follow (it's converted to a mathcode only if it need to be, that is, if TeX is looking for mathcodes, which it doesn't when collecting the parameter text of a called macro).

Why doesn't the version with “ work with pdflatex? Because this is not a seven bit character, so pdftex sees it as two characters (it's even wrong to say \lccode~=“, in pdftex, as a stray character will remain). With XeTeX or LuaTeX, instead, multibyte characters are converted to a single entity before the engine starts the tokenization process.

-
OK, new undisclosed issue: my text editor uses the unicode quotes: “” (which ensures the "right" quoting in the final output). I cannot seem to determine the "begin" and "endquote" in your TeX magic. Is it possible or is you method only suited for the same opening as closing character? –  rubenvb Dec 12 '11 at 13:24
Thanks for the awesome help. –  rubenvb Dec 12 '11 at 13:52
Shouldn't the second " in the \def not also be active? Could you explain the \mathcode"="8000 line a little? –  Martin Scharrer Dec 12 '11 at 15:07
@Martin: No, the second " doesn't have to be active; there is no such restriction on non-parameter tokens in parameter texts. A mathcode of 8000h means math-active: in math mode, the character is treated as if it were active, and TeX uses its meaning. –  Philipp Dec 12 '11 at 23:04
@MartinScharrer It has the correct catcode; nowhere we say \catcode"=13, when the definition of the active dblquote is performed: you see that the final dblquote of the parameter text is outside the \lowercase. –  egreg Dec 13 '11 at 11:43

You can do this by making " active (like e.g. ~ is) and define it as a macro which looks for another " as end-marker. You need to use the TeX \def primitive instead of LaTeX \newcommand macro:

\documentclass{article}

\usepackage{amsmath}

\catcode\"=\active
\def"#1"{\text{#1}}

\begin{document}

"bla"

\text{bla}

$a_{"bla"} = b_{\text{bla}}$

\end{document}


If you want to allow whole paragraphs add a \long before \def.

You have to be careful with packages which also redefine " like babel with some languages. You should have the above code after loading such packages.

-
How would this work in conjunction with Xe(La)TeX in which has special handling in non-math environments? –  rubenvb Dec 12 '11 at 12:48
The above code uses TeX macros and will work with all TeX formats like pdflatex and xelatex. From which special handling are you talking exactly? If there is an specific issues, just edit your question to mentioned it. However, it should just work fine. –  Martin Scharrer Dec 12 '11 at 13:01
yes it does, I have edited the question to include my requirement of "math mode" only. People in the chat room said it was possible. –  rubenvb Dec 12 '11 at 13:02