7

I've just noticed that using $...$ inside an equation have similar results to \text{...} and \mbox{...}:

\[ x $ hello world $ y \]

Maybe due to the way an equation is implemented, this code is synonymous to

$ x $ hello world $ y $

What do you think?


UPDATE

Following the responses, it seems that it's not standard, and this piece of magic requires fleqn and amsmath.

\documentclass[fleqn]{article}

\usepackage{amsmath}

\begin{document}

hello world
\[ x $ hello world $ y \]
\[ x \mbox{ hello world } y \]
\[ x \text{ hello world } y \]

\end{document}

@Rmano pointed out sensitivity to parentheses:

\[ x \left( $ hello world $ \right) y \]

But I say it only supports my theory that the implementation of any eq environment is linked to $. That is, the first $ ends the math part that started with \[, and the second $ opens a new one. Thus, this example doesn't compile for the same reason the following doesn't:

\[ \left( \]
or
$ \left( $ $ \right) $
5
  • 4
    I think you've missed an error message, your first snippet will surely cause an error. Commented Feb 14, 2022 at 21:32
  • 2
    after any error message , if you scroll past the error, any generated pdf is not intended to be usable, just a possible debugging tool. Commented Feb 14, 2022 at 21:33
  • Instead of this wrong approach, load the amsmath package and insert \text{hello world} inside the bounds of math mode. Commented Feb 14, 2022 at 21:35
  • @Mico and David Carlisle, I added an update.
    – Zohar Levi
    Commented Feb 15, 2022 at 7:02
  • I think it still works by accident (it's surprising it doesn't error out), and the spacing after the first display is wrong. Try with \[ x \left( $ hello world $ \right) y \] ...
    – Rmano
    Commented Feb 15, 2022 at 9:48

5 Answers 5

4

Yes, the code in the first display does not produce errors:

\documentclass[fleqn]{article}

\usepackage{amsmath}

\begin{document}

hello world
\[ x $ hello world $ y \]
\[ x \mbox{ hello world } y \]
\[ x \text{ hello world } y \]

\end{document}

It seems to produce equivalent results to the latter two, but just by chance and because how fleqn is implemented.

The fact that it doesn't show errors does not mean it is correct input. It is really wrong and should not be used.

Why does it work? Because \[ initiates display math mode, but then the fleqn option makes LaTeX into building a box in text mode in which math mode is reinitiated. This is where the chance happens. However, the fact that this construction produces errors when fleqn is not in force should tell you that it's wrong. And it is.

8
\documentclass{article}

\begin{document}

text
\[ x $ hello world $ y \]
text

\end{document}

Produces

! Display math should end with $$.
<to be read again> 
                    
l.6 \[ x $ 
           hello world $ y \]
?

At that point is it best to type x to stop the run and fix the error.

If you scroll past any error (or your editor ran tex in nonstopmode so tex always scrolls past errors) you may possibly be able to use the PDF as a debugging aid but it is never usable as a document.

In this case you get

enter image description here

Which is clearly unusable. The display math just has x the hello world comes after the display not in it, the y which was entred in display math is typeset in inline math, and the text that was entered after the math is typeset in math mode.

7

You wrote,

I just noticed that using $...$ inside an equation [has] similar results to \text{...} and \mbox{...}.

I guess the correctness of this claim depends critically on what you consider to be "similar results". Consider the following sample document:

\documentclass{article}
\begin{document}
\hrule 
\[ x $ hello world $ y \]  % this is line 4 of the sample doc.
\smallskip
\hrule
\[ x \mbox{ hello world } y \]
\hrule
\end{document}

Running this code generates the following error messages:

! Display math should end with $$.
<to be read again> 
                    
l.4 \[ x $ 
           hello world $ y \]
? 

! LaTeX Error: Bad math environment delimiter.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.4 \[ x $ hello world $ y \]
                             
? 
! Missing $ inserted.
<inserted text> 
                $
l.6 \hrule
          
? 

and the resulting output looks like this:

enter image description here

Speaking for myself, I don't think these two results look all that similar. Indeed, the upper half of the screenshot looks like a full-blown typographical disaster, while the lower half looks exactly what I would expect to obtain from typesetting x \mbox{ hello world } y in display-math mode.

If you are unsure of how TeX's inline-math and display-math modes differ, feel free to post a new query.

1

The new option you added effectively does not error out, but it's just by accident. Try:

\[ x \left( $ hello world $ \right) y \]

...and watch it exploding. 😉

Basically, that syntax is wrong (not supported nor expected). It's like when you have "undefined behavior" in programming languages: everything can happen, even that it works (for small values of "working").

0

In a guts-of-LaTeX sense, you're not wrong when you say "the implementation of any eq environment is linked to $".

As defined by latex.ltx, \[ is a macro which (ignoring \protect gunk) expands to

\relax
\ifmmode
  \@badmath
\else
  \ifvmode 
    \nointerlineskip \makebox [.6\linewidth ]{}
  \fi
  $$
\fi

That is, first it checks whether we're already in math mode, and if so it issues an error. Otherwise, it checks if we're in vertical mode, and if so it messes with the spacing a bit. And then (whether or not we were in vertical mode) it invokes $$ to go into display math mode. amsmath changes things up a bit -- \[ becomes shorthand for \begin{equation*}, which is a real environment -- but the action of putting the engine into display math mode is still done by $$.

This is, at its root, a limitation in TeX proper: $$ is the only way to get into display math mode (and $ is the only way to get into inline math mode). There is no \hbox{}-like primitive control sequence that typesets its argument in (any sort of) math mode. If Knuth had it all to do over again today, perhaps he would do it differently, but this is how it is.

Having said that, something else is also going on, because if I feed this document to plain TeX, I get a hard error:

$$ a $ plus $ b $$
\bye

==>

! Display math should end with $$.
<to be read again>

<*> $$ a $
           plus $ b $$

I am surprised to hear that amsmath+fleqn suppresses this error, I doubt it was on purpose, and I haven't the foggiest idea how it's doing it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .