Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to show Prefix notation in my document, somewhat like this:

+ (x 5 3) (- 6 4)

However, I want to enter it in maths mode, but maths mode removes all the spaces, so I get:

+(x53)(-64)

Which has rather a different meaning. Obviously I could add spaces explicitly, but

$+\;(\times\;5\;3)\;(-\;6\;4) $

is not exactly readable in the source.

Any better ways of doing this?

share|improve this question
1  
Just a thought: this is actually a question about preserving spacing in math mode, so it might help if you edit the title to reflect that. The fact that you're doing it because of prefix notation doesn't matter so much. –  David Z Jul 30 '10 at 8:01
2  
I think the title is fine. I'd rather have questions about high level topics (e.g prefix notation or s-expressions), rather than low level details (which may or may not be the answer to the question). People will probably search by topics. –  Juan A. Navarro Jul 30 '10 at 8:30
    
"Typesetting expressions in prefix notation"? –  mbq Jul 30 '10 at 12:50
2  
BTW, if you're formatting code, you should use an environment for displaying code verbatim (e.g. verbatim or listings), not math mode. –  ShreevatsaR Jul 30 '10 at 14:43
    
The whole point of prefix notation is that you don't need the parentheses. so why do you have them? –  Seamus Nov 15 '11 at 11:13

2 Answers 2

up vote 6 down vote accepted

What you want to write is something very similar to s-expressions. I was thinking some package might already be there to help you write these, but couldn't find anything after some googling. Anyway, the following TeX macro seems to do the work

\makeatletter
\def\sx@stop{\relax}\def\sx@open{(}\def\sx@close{)}
\def\sx@scan#1{%
  \def\sx@arg{#1}%        save the current token
  \ifx\sx@arg\sx@stop   % is it the end of the list?
    \let\sx@next\relax
  \else
    \ifx\sx@arg\sx@close\else\sx@space\fi % add space unless token is ")"
    {\sx@arg}%                            % write the token
    \ifx\sx@arg\sx@open                   
      \let\sx@space\relax                 % kill next space if token is "("
    \else
      \let\sx@space\;
    \fi
    \let\sx@next\sx@scan                  % scan next token
  \fi\sx@next
}
\newcommand{\sexp}[1]{\let\sx@space\relax\sx@scan#1\relax}
\makeatother

\begin{document}
$\sexp{+ (\times 5 3) (- 6 4 {7 8})}$
\end{document}

Note that you can also group elements, like {7 8} to momentarily switch off the effect of the command (i.e. it shows as "78" in the output). You might also need this if some symbol in the expression is a complicated macro that takes arguments or whatever, just enclose the whole thing in a single {group}.

share|improve this answer
1  
If there isn't a s-expressions package, would you consider turning this code into one and uploading it to CTAN? –  Seamus Aug 24 '11 at 13:40

I would just define a macro \newcommand{\prefix}[3]{#1\;#2\;#3}, and then use it like this:

$\prefix{+}{(\prefix{\times}{5}{3})}{(\prefix{-}{6}{4})}$

Is it readable enough is a personal preference.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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