[texhax] very long nested expressions in math-mode

Torsten Anders torstenanders at gmx.de
Mon Oct 17 15:20:18 CEST 2005


Dear all,

Firstly, I should mention that I am new to this list. I read the FAQ 
and quite some docomentation to find an answer to my problem, without 
success unfortunately. Nevertheless, I apologise in case I missed 
something obvious.

I want to transform program source examples into mathematical notation 
in Latex (tetex, i.e. incl. AMS packages), because that way the 
examples become more concise and better to communicate. Because the 
source is based on functional and logic programming, the available 
algorithm packages are less suited -- the result is too easily misread 
as procedural programming. Instead, I would prefer to write plain math 
expressions.

In short, my problem is that I need to write rather long expressions 
(e.g. highly nested and with long variable names for 
comprehensibility). Thus, line breaks are inevitable. Naturally, the 
continued expression should be indented to indicate the nested 
structure of the expression. That is, I don't what to mechanically 
indent by a certain amount (e.g. by \quad), but the indentation should 
depend on the indentation depth of the surrounding, e.g., function 
call. For instance, a short dummy example may read like this:

xs = find(map([1, 2, 3],
                         f : f(x) = x^3),
               isEven)

The most easy way to do something like this is probably to use 
mono-space fonts in some verbatim environment. However, I would prefer 
to write my example in Latex's math mode: e.g., I would prefer to use 
signs like \vee, \wedge, \bigwedge etc.

To avoid getting lost in my own long Latex examples, I would like to 
define some suitable Latex command, e.g., to get the example above I 
would like to write something like

$xs = \Apply{find}{\Apply{map}{[1,2,3], \\ f : f(x) = x^3}, \\ isEven}$

So far I did not find a way to define \Apply as above and get a 
suitable output. In the following, I present a few of my attempts to 
set my examples. However, none of what I came up with so far is really 
satisfying. Perhaps anybody here can help me..

Here comes my first naive attempt to define \Apply:

\newcommand{\Apply}[2]{\ensuremath{#1 ( \begin{aligned}[t] #2 
\end{aligned} ) }}

which is called

$xs = \Apply{find}{&\Apply{map}{&[1,2,3], \\ &f : f(x) = x^3}, \\ 
&isEven}$

The additional '&' makes the code slightly less readable, but the 
actual problem is that everything after the aligned environment (i.e. 
the comma and the cloing parenthesis) 'go back' on the original line 
like

xs = find(map([1, 2, 3],	 ),)
                         f : f(x) = x^3
               isEven

Next try:

\newcommand{\Apply}[3]{\ensuremath{#1 ( \begin{aligned}[t] #2 ) #3 
\end{aligned} }}

$xs = \Apply{find}{&\Apply{map}{&[1,2,3], \\ &f : f(x) = x^3}{,} \\ 
&isEven}{}$

This results in the desired output, but for complex examples with 
parenthesis/brackets/curly braces in the third \Apply argument this 
becomes very hard to read and the editor (i.e. emacs) can't help 
anymore.

To avoid the problem that after a box -- which starts at the current 
line but introduces some line breaks -- the current line is _not_ 
continued I tried to center the whole expression:

\newcommand{\Apply}[2]{\ensuremath{#1 \left( \begin{aligned} #2 
\end{aligned} \right) }}

$xs = \Apply{find}{&\Apply{map}{&[1,2,3], \\ &f : f(x) = x^3}, \\ 
&isEven}$

The structure of the resulting expression is still clearly readable and 
the code is also more easy to write. However, this layout can also 
easily be misread: instead surrounding the arguments of a function call 
the parenthesis look more like a binom (and reducing the gap between 
the function name and the parenthesis does not really overcome that).  
:-P

Does anybody have a better idea how to set very long nested 
expressions? Any help is most appreciated!

Best wishes,
Torsten

--
Torsten Anders
Sonic Arts Research Centre
Queen's University Belfast (UK)
www.torsten-anders.de



More information about the texhax mailing list