2

I often find myself doing things like

\[
\begin{tabular}{cc}
foo & bar \\
baz & qux
\end{tabular}
\]

or

\[
\begin{verbatim}
code snippet
\end{verbatim}
\]

to put a table or code snippet into my document without making it float. I'm using \[...\] because I like the way it centres its contents and leaves a little vspace above and below it. But of course \[...\] also puts me in math-mode, which is not what I want.

What's the equivalent of \[...\] that doesn't put me into math-mode?


In fact (if I may delve into 'opinion-based' territory for a moment), I am of the opinion that \[...\] should not enter into math-mode at all. Consider something like:

Finally, our equation becomes
\[
x^2 = y^2 - z^2,
\]
which is easily proved.

I often want to include a comma (or a full-stop, or whatever) with the equation, in order to make it flow with the rest of the surrounding text. But the comma should not be typeset in math-mode, because it is not a mathematical comma, it is a textual comma. So what I really want to type is:

Finally, our equation becomes
\begin{some-suitable-environment}
$x^2 = y^2 - z^2$,
\end{some-suitable-environment}
which is easily proved.

so that I can delimit the piece of the centred text that is actually math. Is this a sensible suggestion? Does some-suitable-environment exist?

  • \[\begin{verbatim} produces ! Missing $ inserted. <inserted text> $ l.5 \begin{verbatim} – David Carlisle Apr 23 '15 at 20:55
  • to place a comma in the surrounding text font, just use \[a=b \text{,}\] – David Carlisle May 5 '15 at 14:28
  • @DavidCarlisle Mm, that works. But, speaking idealistically rather than pragmatically, it's a bit dissatisfying to have to switch from the surrounding text-mode, to math-mode (with \[...\]) and back to text-mode (with \text{...}). In other words, I would prefer the document markup to emphasise that the comma is not part of the equation. – John Wickerson May 5 '15 at 14:32
  • You appear to have changed the question, thus invalidating existing answers. Your original question was (mostly) about non-math usage and for example gave an example of verbatim (which, along with any other display environment will not work with your answer) If you want to ask about punctuation in math displays that is a different question and a duplicate of the related question in the right sidebar What could be more highlighting that it is not math than a command called \text ? – David Carlisle May 5 '15 at 14:38
  • If you want a text comma (but why would you?), just type \text{,}. You're “solving” a problem creating many others: if you try your solution with \sum in the formula and compare it with a standard displaymath environment, you'll see what I mean. – egreg May 5 '15 at 18:00
4

\begin{center}...\end{center}

Is the non math centred display environment. (But don't use it with $ to fake a centred math display, the spacing will not be correct).

  • Thanks David. Mm, I thought {center} might have something to do with things. But as you say, the vertical spacing is quite different from {displaymath}, so I don't like to use it. – John Wickerson Apr 23 '15 at 13:34
  • 1
    @JohnWickerson using \[ or displaymath is simply wrong. center is what you should use. It uses the class defined space parameters for displays as shared with verbatim and quote and any other display environment (which you can change in the document if needed) – David Carlisle Apr 23 '15 at 13:58
1

This may be help you to find a way

\documentclass[twocolumn]{article}

%  just for grid
\usepackage{tikz}
\usepackage{eso-pic}

\AddToShipoutPicture{%
\begin{tikzpicture}[overlay,remember picture]
  \draw[blue!20!white,thin]
       (current page.south west) grid [xstep=10mm,ystep=4mm] (current page.north east);
\end{tikzpicture}}

\makeatletter
\newenvironment{mtc}{%
\@@line\bgroup\hss}{\hss\egroup\vspace{\belowdisplayshortskip}}
\makeatother

\begin{document}
bla bla bla bla
\[x+y=z\]
bla bla bla bla

bla bla bla bla
\begin{equation}
 2x+2
\end{equation}
bla bla bla bla
\begin{center}
bla bla bla bla
\end{center} 
\newpage
bla bla bla bla

\begin{mtc}
$x+y=z$
\end{mtc}
bla bla bla bla

bla bla bla bla

\begin{mtc}
bla bla
\end{mtc}
bla bla bla bla

\begin{mtc}
bla bla bla bla
\end{mtc} 

\end{document}

enter image description here

1

You can easily create a new environment that starts a display math, but typesets its contents in text mode.

\newsavebox{\wickersondisplaybox}

\newenvironment{wickersondisplay}
 {\[\begin{lrbox}{\wickersondisplaybox}}
 {\end{lrbox}\usebox{\wickersondisplaybox}\]}

\newcommand{\<}{\begin{wickersondisplay}}
\renewcommand{\>}{\end{wickersondisplay}} % \> is preempted

But it would be the wrong thing to do: use center and be happy with it.

Note also that the output of

\<
$\sum_{k=0}^n k=\frac{n(n+1}}{2}$,
\>

is very different from

\[
\sum_{k=0}^n k=\frac{n(n+1}}{2}\text{,}
\]

If all you need is a “text comma”, just use \text{,}.

0

Have you checked out the listings package? It's designed for code samples. There are lots of options, e.g. telling LaTeX what language the code sample is in, so that it can highlight stuff appropriately.

By default, the code sample takes up the whole page width. Is that a satisfactory form of centering?

output

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage{listings}          %allow code samples
%\usepackage{caption}           %for the captionof command

%This makes all code samples have these settings by default
%Overwrite these settings by restating them in each code sample
\lstset{
    frame=single,            %outline the code
    breaklines=true,         %break apart long lines onto multiple lines
    numbers=left,
    %float=H,                %make it a float, and put it right Here
    %columns=flexible,       %makes code copy and pastable
    %language=C,             %custom language (C, HTML, TeX, Java)
    showstringspaces=false,  %hide visable 'space' markers
}

%this will make LaTeX refer to "code samples" instead of "program listings", when you caption listings
\renewcommand{\lstlistingname}{Code Sample}

\begin{document}

blah blah. Here's some text so that we can see that the listing is centered.
blah blah. Here's some text so that we can see that the listing is centered.

\begin{lstlisting}
code goes here
\end{lstlisting}

blah blah. Here's some text so that we can see that the listing is centered.
blah blah. Here's some text so that we can see that the listing is centered.

\end{document}
  • Thanks falsePockets. When I said "code snippet", I guess I had in mind a few words, rather than a few lines. Left-align makes sense once you have multiple lines, but I tend to have a smaller expression like "if B then C else D" so I just want it centred. – John Wickerson Apr 23 '15 at 13:37
  • @JohnWickerson -- you could use \[ \text{...} \] if you are using amsmath. – barbara beeton Apr 23 '15 at 15:35
0

Following on from Barbara's, touhami's and David's suggestions, I came up with this implementation:

% Define {center} environment with math-mode spacing (
\def\<{\begin{displaymath}\mbox\bgroup}
\def\>{\egroup\end{displaymath}}
% )

which is used like so

Finally, our equation becomes
\<
$x^2 = y^2 - z^2$,
\>
which is easily proved.

and ensures that the comma after the equation is set in the correct (text-mode) font.

  • If you use \mbox\bgroup the argument of \mbox is just \bgroup not the tokens between \bgroup and \egroup the fact that it sort of accidentally produces something that does not generate an error is an accident of the expansion process. Your example is now using display math vertical spacing but inline math setting, which is odd to say the least. – David Carlisle May 5 '15 at 14:06
  • Oh. I thought it a bit odd that \mbox works in this definition but \text doesn't. Thanks for the tip about the display/inline -- I guess I would need a \displaystyle in there somewhere. Hm, all a bit of a mess! – John Wickerson May 5 '15 at 14:34
  • It's a "mess" because it is fundamentally abusing display math, so further additions to try to hide that just add complication on complication. – David Carlisle May 5 '15 at 14:40
  • 1
    @JohnWickerson If that's your only worry, why not \makeatletter \begingroup\lccode`\~=`\,\lowercase{\endgroup\def~}{\@ifnextchar\]{\text{,}}{\mathpunct{\mathchar`\,}}} \makeatother \AtBeginDocument{\mathcode`\,="8000 }? That is, make , math active to check if the next token is \] and, in that case, output a “text comma”. – Manuel May 5 '15 at 19:05
  • 1
    @Manuel Also \end{equation}, \end{align}, \end{gather} and so on, with the problem that you can't check for \end{align} following the comma. Too complicated. – egreg May 6 '15 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.