4

I realize this is a bit of an odd request.

I'd like to typeset some characters in math mode (such as 1 \land \lnot 2 \lor 3), and have them appear equally spaced, rather than with automatic spacing. By default, this code produces

You can see the \lnot is right up next to the 2. And when multiple numbers are together they're close to each other as well. How can I make these spaced evenly so there's no distinction between characters?

jtbandes
  • 4,262
  • While I agree that the automatic spacing is the way to go, there are possibilities to introduce space (if you really, really must :) ): Check the table from this answer for completeness. – Count Zero Nov 08 '11 at 09:12

5 Answers5

6

That's not good style. The automatic spacing should be the right amount. But if you really want it: One way to do this would be to put everything in its own math mode:

\documentclass{article}

\begin{document}

$ 1 \land \lnot 2 \lor 3 $  % for comparision

$1$ $\land$ $\lnot$ $2$ $\lor$ $3$

\end{document}

Result

(I put the numbers also in math mode to ensure that always the correct math font is used. By default this doesn't make any difference, though.)


To have the $s added automatically you can use a loop which reads every number, character or macro one by one. Simply group a number with multiple digits together:

\documentclass{article}

\makeatletter
\newcommand\equspacedmath[1]{%
  \mbox{% to ensure text mode
    \@tfor\L:=#1\do{%
        $\L$\space
    }%
  }%
}
\makeatother

\begin{document}

\equspacedmath{1 \land \lnot 2 \lor 3}

\equspacedmath{1 \land \lnot {23} \lor 4}

$ \equspacedmath{1 \land \lnot 2 \lor 3} $

\[ \equspacedmath{1 \land \lnot {23} \lor 4} \]

\end{document}
Martin Scharrer
  • 262,582
  • 1
    I know it's not good style; the point here is that the sequence should be a string of characters, not a mathematical/logical expression. Thanks for the tip... do you know of a way to do it without so many dollar signs to switch in and out of math mode? – jtbandes Nov 08 '11 at 09:08
  • You could use a loop to add the $s automatically. See my update. – Martin Scharrer Nov 08 '11 at 09:14
  • Would "equal spacing" not require you to use ~ rather than \space to avoid stretchability/shrinking? – Werner Nov 08 '11 at 18:43
  • @Werner: In principle yes, but I have a \mbox around it anyway, so there should be no stretching. – Martin Scharrer Nov 09 '11 at 09:07
  • It's not really accurate to say "the automatic spacing should be the right amount". The user always has the last work on what the right amount is. Often LaTeX gets it wrong for non-standard (but entirely reasonable) use-cases. – Roly Sep 03 '13 at 17:10
  • @Roly: It's also not really accurate to say "The user always has the last work (word?) on what the right amount is". For example if the user is a student then the supervisor has the last work and might not be happy at all about the spacing. – Martin Scharrer Sep 04 '13 at 14:00
  • @MartinScharrer Sorry, yes: "word", not "work". I'm not getting your point, though. (Just call the "supervisor" the "user" and my point remains.) – Roly Sep 04 '13 at 18:03
4
\documentclass[a4paper]{article}

\makeatletter
\newcommand{\formula}[2][\the\thinmuskip]{%
  \hbox{$\thinmuskip=#1\relax\@formula#2\@endformula$}}
\def\@formula#1{%
  \def\@tempa{#1}
  \ifx\@tempa\@endformula
    \expandafter\relax
  \else
    \mathop{\kern0pt#1}
    \expandafter\@formula
  \fi}
\def\@endformula{\@endformula}
\makeatother

\begin{document}
\formula{1 \land \lnot 2 \lor {333}\lor A}
\end{document}

You have to group numbers that should be kept as a unit; also commands with arguments (such as \mathbf{x}) should be put in braces. You can set a different spacing (I've chosen the usual thin space) by changing \the\thinmuskip to some other value in terms of mu units or by giving an optional argument to \formula:

\formula[6mu]{...}

would have a spacing double than the default.

Basically, we search one token at a time and typeset it as a mathop: between two consecutive mathop atoms TeX always puts \thinmuskip space.

For simple formulas this might be enough; other things can be accommodated by adding code in the \else part. However I'd use this convention only with direct or reverse Polish notation.

egreg
  • 1,121,712
2

insert some space

$ 1 \land {\lnot\,} 2 \lor 3 $ 


You can define \Lnot if you need it more than once

enter image description here

1

You could redefine the \lnot command to insert a "thickspace", \;, after itself:

\documentclass{article}
\let\lnotold\lnot
\renewcommand\lnot{\lnotold\;}
\begin{document}
$a \land \lnot b \lor c$
\end{document}

enter image description here

Mico
  • 506,678
1

You could also use the listings package which should ensure that the spacing in between does not shrink or get stretched:

enter image description here

\documentclass[border=2pt]{standalone}
\usepackage{xcolor}
\usepackage{listings}
\lstset{basicstyle=\ttfamily, mathescape=true}

\begin{document}
\lstinline{1 $\land$ $\lnot$ 2 $\lor$ 3}
\end{document}
Peter Grill
  • 223,288