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?

  • 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 8 '11 at 9:12
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}
  • 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 8 '11 at 9:08
  • You could use a loop to add the $s automatically. See my update. – Martin Scharrer Nov 8 '11 at 9:14
  • Would "equal spacing" not require you to use ~ rather than \space to avoid stretchability/shrinking? – Werner Nov 8 '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 9 '11 at 9: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 3 '13 at 17:10
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.

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

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}

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.