# Is there any difference between quad in math mode and text mode?

Is there any difference between $a \text{\quad text\quad} b$ and $a \quad \text{text} \quad b$? I cannot see the difference as shown in the following figure.

If there is no difference, which one should I use for the sake of best practice?

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{amsmath}
\begin{document}
$a \text{\quad text\quad} b$\\
$a \quad \text{text} \quad b$
\end{document}


I want to use this when solving $(x-a)(x-b)=0$. The solution is usually written in gather* as follows

\begin{gather*}
(x-a)(x-b)=0\\
\end{gather*}


or

\begin{gather*}
(x-a)(x-b)=0\\
\end{gather*}

-
A third variant to consider would be $a$\quad text\quad$b$. It produces the same output as do the first two variants... –  Mico Nov 10 '13 at 8:47
Personal opinion (of a newbie): You should put the spaces where they belong. If they are part of the text then put them inside the \text, but in your case they are part of the equation's layout, hence they should go outside of \text. Also, the \quad might be used in other parts of the equation, and having different spaces associated with the same macro may seem odd (in the source and in the output too, probably). –  Bakuriu Nov 10 '13 at 10:29
The accepted answer is subjected to change without notice. I am so sorry for this inconvenience and thank you for your cooperation. –  stalking is prohibited Nov 10 '13 at 14:34

Under normal conditions, using \quad inside or outside \text gives the same result, as far as the amount of space is concerned. I'm disregarding what happens when \text is used in a sub/superscript, for the moment.

TeX maintains two current fonts when it's typesetting a math formula: it remembers the font that was current before the formula started and also takes note of the current math family so that math symbols which are given type 7 are typeset using the font corresponding to the current family. Note that the current math family is only saved as a number; this number will be turned into a real font only when the formula is finished and is being converted into a horizontal list.

When an instruction such as \quad is found in a formula, it expands to \hskip1em as usual and the em is converted into a length by examining the fontdimen 6 parameter of the current text font, so the first that was mentioned above. If \mskip18mu were used, the em of the font attached to math family 2 would be used; but it's not.

When \text{text} is processed in math mode, TeX executes (in a group)

\mathchoice
{\textdef@\displaystyle\f@size{text}}%
{\textdef@\textstyle\f@size{\firstchoice@false #1}}%
{\textdef@\textstyle\sf@size{\firstchoice@false #1}}%
{\textdef@\textstyle \ssf@size{\firstchoice@false #1}}%


and we're interested in the first and second arguments to \mathchoice. The \firstchoice@false command is just to avoid executing multiple times commands such as \label or \stepcounter that can possibly sneak in the argument to \text, but it's irrelevant for the problem at hand.

For the sake of simplicity, let's assume we're in \displaystyle (but for \textstyle it would be the same). TeX typesets four boxes, but uses only the one resulting from

\textdef@\displaystyle\f@size{text}


This is turned into

\hbox{{\everymath{\displaystyle}\let\f@size\f@size\selectfont text}}


We have to remember that \f@size, \sf@size and \ssf@size expand to the font sizes LaTeX has determined to be the current overall font size (they are 10pt, 7pt, 5pt when the size is \normalsize in a 10pt document; they are 12pt, 8pt, 6pt when the size is \normalsize in a 12pt document or \large in a 10pt document; and so on). In this case the \let does nothing, but \selectfont is really relevant!

What does it do? It resets the current text font based on the current values of \f@encoding, \f@family, \f@series, \f@shape, \f@size and \f@baselineskip. In this case, since \f@size has not changed, the same font that was current outside \text is carried over inside it.

As a conclusion, \quad will have the same width inside \text as it has outside it.

If \text is used in a first level subscript, only \f@size changes, becoming equivalent to what \sf@size was before, so the \quad will use the width corresponding to the current text font, but at \sf@size.

Of course, using \quad outside \text has another difference: the argument to \text is typeset in a box, so this glue cannot be used as a break point if the formula is inline. (This was remarked by David Carlisle.)

Why the example by Przemysław Scherwentke gives different quads? When we do

\font\meta=logo10 at 20pt \meta


we're overriding the NFSS infrastructure for font selection: \meta will become the current text font, but the values of the \f@... macros is not changed. Therefore for the quads in

\meta $a \quad\text{META}\quad b$


the value corresponding to \meta are used (it is the current text font). Conversely, in

\meta $a \text{\quad META\quad} b$


the determination of 1em happens after \selectfont, so the font is reset to what it was before the \meta instruction, because the \f@... macros are not updated when issuing \meta. Indeed “META” is typeset in the current text font, not using the logo font.

Here's a test.

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\font\meta=logo10 at 14.4pt
{\meta $a \quad\text{META}\quad b$}

{\meta $a \quad\mbox{META}\quad b$}

{\meta $a \text{\quad META\quad} b$}

{\meta $a \quad\text{META}\quad b$}

\newcommand{\goodmeta}{%
\fontencoding{U}%
\fontfamily{logo}%
\fontsize{14.4}{14.4}%
\selectfont}
{\goodmeta $a \quad\text{META}\quad b$}

{\goodmeta $a \quad\mbox{META}\quad b$}

{\goodmeta $a \text{\quad META\quad} b$}

{\goodmeta $a \quad\text{META}\quad b$}

\end{document}


One can see that in lines 1, 3 and 4 the word “META” is typeset in Computer Modern, while in line 2 it's in the logo font (because \mbox uses the current font without issuing any \selectfont command).

In the other cases, where \goodmeta is used, the results are identical.

-

In LaTeX, the definition of the macro \quad (stated in latex.ltx) is:

\def\quad{\hskip1em\relax}


Note that the macro works the same way in both text and math mode. (The macros \hskip and \relax are TeX "primitive" commands.)

Thus, as long as 1em has the same value in text and math mode -- which will be the case if you're using a common font family and font size for math and text (which, happily, is the case for the MWE you've posted) -- you can use either variant and get the same output. You should thus feel free to use whichever variant feels most natural to you. To me, \quad\text{or}\quad seems a bit more natural than \text{\quad or\quad} does...

Addendum: As @PrzemysławScherwentl has demonstrated in a separate answer, it is possible -- say, through the use of a PlainTeX-type font-related instruction -- to create a setup in which the value of 1em is not the same in math and text mode. His example thus serves as an important qualifier.

More generally, whenever you use different font families for text and math (possibly even at different sizes), you will find that the two variants do not produce the same output.

-
You don't have to use primitive font syntax, you'd get the same with any font change that did not reset the math fonts, so most latex font changes. Also you get different behaviour due to boxing, even with no font change. –  David Carlisle Nov 10 '13 at 11:21
@DavidCarlisle - Thanks. I didn't mean to imply that the only way to get different-sized ems was through "primitive" font syntax, but merely that this one one possibility for obtaining different-sized ems. Thanks for also pointing out the effect that \text{\quad} vs \quad when encountering, say, a line break. –  Mico Nov 10 '13 at 11:26
I tend to make special extra \text macros which has \quad, \qquad etc, build in, then the typographic spacing I'd like to have in my equations does not polute my code –  daleif Nov 10 '13 at 11:46

\quad is an hskip of 1em and its definition is the same in math and text mode, however there are many differences between the two uses you give. Notably em is a font dependent unit so the actual size depends on the fonts in use, but perhaps more importantly \text is a box command so the use inside \text is inside a box and will not be discarded at a line break:

\documentclass{article}
\setlength\textwidth{2cm}
\usepackage{amsmath}
\begin{document}

\noindent X\dotfill X

\noindent aaa aaa  $a + \text{\quad text\quad} b$

\noindent aaa aaa $a + \quad \text{text} \quad b$

\end{document}

-

A counterexample (?)

\documentclass{article}

\usepackage{amsmath}
\begin{document}
\noindent
$a \text{\quad text\quad} b$\\
$a \quad \text{text} \quad b$

\font\meta=logo10 at 20pt
\noindent
\meta
{$A \text{\quad META\quad} M$\\
$A \quad \text{META} \quad M$}
\end{document}


This example is not very natural, but shows that the answer is YES, if em's are equal.

-
Ingenious. I've modified my answer to highlight the relevance of using a common font family (and at a common font size) in order for \quad to generate the same output in text and math mode. –  Mico Nov 10 '13 at 9:57
This is because a font defined with \font doesn't obey LaTeX's rules; \text basically issues \usefont{\f@encoding}{\f@family}{\f@series}{\f@shape}, but since \meta doesn't change any of the \f@... macros, the result is almost unpredictable: in the outer level of the formula the current text font quad (\meta) is used, but not inside \text. Indeed you get META in the normal text font. –  egreg Nov 10 '13 at 11:59