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 am a long time user and fan of LaTeX, but I hate the syntax! I wish it could be more like Python or Haskell. The syntax of those programming languages is much more readable than LaTeX which is ironic because you are much more likely to spend time reading raw LaTeX than programming code.

I would like it to be something like

We have
\equation
    A =
    \bmatrix
        a & b
        c & d
where ...

Instead of

We have
\begin{equation}
    A =
    \begin{bmatrix}
        a & b \\
        c & d
    \end{bmatrix}
\end{equation}
where ...

This is a simple example but you all know how onerous the curly brackets \begin's and \end's can get in complicated examples

Is there an alternative interpreter for LaTeX that has nicer syntax?

share|improve this question
3  
You can use a different markup language like Markdown. Pandoc can create LaTeX files. However you loose flexibility. –  Marco Jan 9 '12 at 0:49
    
A hate for LaTeX syntax on a TeX & LaTeX Q&A is a very strong word to use, don't you think? –  Werner Jan 9 '12 at 0:50
3  
@Werner Yes! It's like: I love my wife, but hate the way she bites her nails ... I still love my wife. :) –  Stephen Mc Ateer Jan 9 '12 at 0:57
1  
@StephenMcAteer: Touché, as I bite my own nails... :-| –  Werner Jan 9 '12 at 1:02
6  
Actually I would hate it if LaTeX had Python-style significant indentation. –  celtschk Jan 9 '12 at 1:26

3 Answers 3

up vote 14 down vote accepted

You need a very recent expl3 (January 8th, 2011) for the following code to work (I added \ior_str_map_inline:nn a few minutes ago).

\RequirePackage{expl3,l3str}
\ExplSyntaxOn
\bool_new:N \g_strange_tmp_bool
\tl_new:N \g_strange_last_line_tl
\tl_new:N \g_strange_line_tl
\int_new:N \g_strange_indentation_int
\int_new:N \g_strange_last_indentation_int
\seq_new:N \g_strange_indentation_seq
\seq_new:N \g_strange_environments_seq
\tl_new:N \l_strange_tmpa_tl
\use:n
  {
    \ExplSyntaxOff
    \int_set_eq:NN \tex_endlinechar:D \c_minus_one
    \ior_str_map_inline:nn { \c_job_name_tl }
      {
        \tl_gset_eq:NN \g_strange_last_line_tl \g_strange_line_tl
        \int_gset_eq:NN \g_strange_last_indentation_int \g_strange_indentation_int
        \bool_if:NTF \g_strange_tmp_bool
          {
            \tl_gset_rescan:Nnn \g_strange_line_tl { } {#1~}
            \int_gset:Nn \g_strange_indentation_int
              { \str_length:n {#1} - \exp_args:No \str_length:n { \use:n #1 } }
            \int_compare:nTF { \g_strange_indentation_int > \g_strange_last_indentation_int }
              {
                \seq_gpush:Nx \g_strange_indentation_seq
                  { \int_use:N \g_strange_last_indentation_int }
                \seq_gpush:Nx \g_strange_environments_seq
                  { \str_substr:Nnn \g_strange_last_line_tl {1} {-1} }
                \seq_get:NN \g_strange_environments_seq \l_strange_tmpa_tl
                \exp_args:No \begin { \l_strange_tmpa_tl }
              }
              {
                \tl_use:N \g_strange_last_line_tl
                \int_while_do:nn
                  { \g_strange_indentation_int < \g_strange_last_indentation_int }
                  {
                    \seq_gpop:NN \g_strange_indentation_seq \l_strange_tmpa_tl
                      \int_gset:Nn \g_strange_last_indentation_int \l_strange_tmpa_tl
                    \seq_gpop:NN \g_strange_environments_seq \l_strange_tmpa_tl
                      \exp_args:No \end { \l_strange_tmpa_tl }
                  }
              }
          }
          {
            \str_if_eq:xxT { #1 } { \token_to_str:N \endinput }
              { \bool_gset_true:N \g_strange_tmp_bool }
          }
      }
  }
\endinput

\documentclass{article}
\usepackage{amsmath}
\document
    We have
    \equation
        A =
        \bmatrix
            a & b \\
            c & d
    where ...
%

The trailing % at the same indentation level as \document is very important. I decided to keep \\ to separate lines of the matrix, because supporting that would require changing the definition of \bmatrix. Here, you simply need to include the code before the document starts, and then stop worrying about it.

share|improve this answer
    
Quite a few bugs, sorry. I'm fixing that. –  Bruno Le Floch Jan 9 '12 at 3:02
    
I'm flabbergasted. At best I was hoping for something like "yeah, that'd be nice but it's not going to happen". I was expecting "go away, the syntax is great". An actual implementation ... jaw dropping. Thank you. –  Stephen Mc Ateer Jan 9 '12 at 7:05
    
@StephenMcAteer I didn't add good support for comments (i.e., it may or may not work), and starred environment can be trouble. Sharpie's answer is probably the correct one here: use a format such as Pandoc, which can then be converted to LaTeX for typesetting. –  Bruno Le Floch Jan 9 '12 at 17:52

Is there an alternative interpreter for LaTeX that has nicer syntax?

In general, TeX is very poor when it comes to "alternative interpreters" due to the fact that writing a program that can understand the grammar of TeX is a very, very hairy problem (and turing-complete too).

Rather than looking for an "alternate implementation" of TeX it's self, try looking for an "alternate grammar" that is close to your ideal input that can be compiled to TeX. Pandoc has been mentioned as a tool that can parse light-weight markup languages such as Mardown and reStructured Text and convert them to LaTeX. Pandoc is also written in Haskell and pretty easy to extend.

Or, you could roll your own. Perhaps something like CoffeeScript could be written for LaTeX. CoffeeScript is a language that mashes up the best parts of JavaScript, Python and Ruby syntax yet compiles to JavaScript to spare the overhead of writing a complete language implementation from scratch.

share|improve this answer

I'm not sure where this lies between a comment and an answer. The length pushed it towards the location of the latter.

Here is perhaps a promotion of LaTeX in lieu of your hate for its syntax that matches that of your example to a fair degree.

Whenever you define an environment in LaTeX, for example via

\newenvironment{myenv}
  {<begin myenv>}% \begin{myenv}
  {<end myenv>}% \end{myenv}

it (...LaTeX) defines two commands: \myenv and \endmyenv. To test this, you'll notice an error in your console when trying to compile the following minimal example:

\documentclass{article}
\newcommand{\mycmd}{test}% \mycmd
\newenvironment{mycmd}{hi}{there}% \begin{mycmd}...\end{mycmd}
\begin{document}
\mycmd
\end{document}

That error being

! LaTeX Error: Command \mycmd already defined.
               Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H   for immediate help.
 ...                                              

l.3 \newenvironment{mycmd}{hi}{there}

Even though you didn't define the an additional command by the name of \mycmd. So, using your code snippet, is it very possible to have LaTeX understand exactly what you're after if you use

We have
\equation
  A =
  \bmatrix
    a & b
    c & d
  \endbmatrix
\endequation
where ...

whatever your definition for equation and bmatrix may be (existing and redefined or not). In fact, the following minimal example compiles without problem:

\documentclass{article}
\usepackage{amsmath}%
\begin{document}
We have
\equation
  A =
  \bmatrix
    a & b \\
    c & d
  \endbmatrix
\endequation
where ...
\end{document}

As commented by egreg, there are instances where this my be "surprising" and yield output contrary to one'e expectation. However, the extent of this may not influence you without more detail.

share|improve this answer
    
Did you try \flushright xyz\endflushright, with some text after \endflushright? You'll be surprised. :) –  egreg Jan 9 '12 at 1:04
    
@egreg: Yes, this is very surprising. I guess there are exclusions to the rule. –  Werner Jan 9 '12 at 1:16
1  
@Werner: accessing environments directly the way you do is undocumented, hence package writers may very well assume that you don't do that. E.g., I could have defined \newenvironment{mytt}{\ttfamily}{}. Then doing \mytt Foo\endmytt Bar will fail. –  Bruno Le Floch Jan 9 '12 at 2:54

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.