Skip to content

Simple examples. Step by step

Lev135 edited this page Apr 17, 2022 · 13 revisions

Paragraphs

As well as in LaTeX .ttex file consists of paragraphs, separated by empty lines and wrapped in some environments. So we start from them.

First paragraph
on two lines


Second paragraph
on three
lines

And this paragraph
  will be splitted,
  because it has incorrect
indentation
First paragraph on two lines

Second paragraph on three lines

And this paragraph
will be splitted, because it has incorrect
indentation

As you can see, translator automatically reformats the text. The length of a line in output is limited by 100 charecters by default. In the last paragraph only 2 and 3 lines were merged. The reason is that for the translator it is not one paragraph but three in accordance with indentation. All lines of a paragraph must have the same indentation.

Inline math

This is inline formula: `a + b = c`.
This is inline formula: $a + b = c$.

NB: Inline formula in .ttex format can't be splitted in several lines.

Environments

Foo-bar-baz example

Useless, but I hope demonstrative example:

@Define
  @Environments
    Foo     = @TexBeginEnd "foo"
    Bar     = @Begin       "\\textit{This is a bar:}"
    Baz     = @End         "\\textit{That was baz...}"

@Foo
  some text in foo
  @Bar
    bar in foo
    @Baz
      Baz in bar
    in bar, but not in baz
  in foo....

  And this is also in foo
But now outside all environments.
\begin{foo}
  some text in foo
  \textit{This is a bar:}
    bar in foo
      Baz in bar
    \textit{That was baz...}
    in bar, but not in baz
  in foo....

  And this is also in foo
\end{foo}
But now outside all environments.

Note that @TexBeginEnd NAME option has the same effect that @Begin "\\begin{NAME}" @End "\\end{NAME}"

Math option

@Math option says that everything in environment is in math mode. Math commands will be replaced only in @Math mode:

@Define
  @Environments
    MathEquation        = @TexBeginEnd "equation" @Math
    WithoutMathEquation = @TexBeginEnd "equation"
  @MathCommands
    >=    = "\\geq"

@MathEquation
  a >= b >= c

@WithoutMathEquation
  a >= b >= c
\begin{equation}
  a \geq b \geq c
\end{equation}

\begin{equation}
  a >= b >= c
\end{equation}

Environments with arguments

@Section is one of the most common examples here. @Frame added just for demonstrating environment with many arguments. The only type of arguments for this time is String.

@Define
  @Environments
    Section (name : String) = @Begin "\\section{$name}"
    Frame (start : String) (stop : String) = @Begin "Starting: $start" @End "Finishing: $stop"

@Section "Frame example"
  @Frame "we have no apples yet" "there are two apples now!!!"
    First apple found!

    Second apple found!
\section{Frame example}
  Starting: we have no apples yet
    First apple found!

    Second apple found!
  Finishing: there are two apples now!!!

@Verb option

@Define 
  @Environments
    Tex       = @Verb
    Verbatim  = @TexBeginEnd "verbatim" @Verb
    Verbatim' = @TexBeginEnd "verbatim" @VerbIndent
    Document  = @TexBeginEnd "document"

@Tex
  % Include packages in `@Tex` environment to 
  % prevent translator working with them as paragraph 
  \documentclass[12pt,a4paper,oneside]{article}
  \usepackage[utf8]{inputenc}

@Document
  Hello, \LaTeX! \\
  Verbatim with extra indentation and spaces
  @Verbatim
    \begin{enumerate}
      \item one
      \item two
    \end{enumerate}
  Verbatim without extra indentation and spaces
  @Verbatim'
    \begin{enumerate}
      \item one
      \item two
    \end{enumerate}
  looks much greater!) 
% Include packages in `@Tex` environment to 
% prevent translator working with them as paragraph 
\documentclass[12pt,a4paper,oneside]{article}
\usepackage[utf8]{inputenc}

\begin{document}
  Hello, \LaTeX! \\ Verbatim with extra indentation and spaces
  \begin{verbatim}
    \begin{enumerate}
      \item one
      \item two
    \end{enumerate}
  \end{verbatim}
  Verbatim without extra indentation and spaces
\begin{verbatim}
\begin{enumerate}
  \item one
  \item two
\end{enumerate}
\end{verbatim}
  looks much greater!)
\end{document}

Prefs

Itemize

Itemize is one of the simplest example of Prefs usage. Each item started with - (with mandatory space after '-'). Consequent lines are grouped in itemize TeX environment (@TexBeginEnd "itemize") and each element will be prefixed by \item (@Pref "\\item")

@Define
  @Prefs
    - = @TexBeginEnd "itemize" @Pref "\\item"

Some text
  - one

    the second paragraph in the first item
  - two
      - 2.1
      - 2.2
  - three
      - 3.1
Some text
\begin{itemize}
  \item one

        the second paragraph in the first item
  \item two
        \begin{itemize}
          \item 2.1
          \item 2.2
        \end{itemize}
  \item three
        \begin{itemize}
          \item 3.1
        \end{itemize}
\end{itemize}

Note, that prefix should be indented more than surrounding paragraphs so as not to be merged with them. Items of one prefix group should have identical indentation and there should be no empty lines between them.

Separating pref groups by empty lines

@Define
  @Prefs
    -  = @TexBeginEnd "itemize" @Pref "\\item"

- one
- two
- three

- first
- second
- third
\begin{itemize}
  \item one
  \item two
  \item three
\end{itemize}

\begin{itemize}
  \item first
  \item second
  \item third
\end{itemize}

Separator option

@Sep option can be used, for example, in align* to insert separator \\ after each line

@Define
  @Prefs
    >  = @TexBeginEnd "align*" @Sep "\\\\" @Math

Some text
  > a^2 + b^2 = c^2
Next text
  > a + b &= c
  >     d &= e + f
Some text
\begin{align*}
  a^2 + b^2 = c^2
\end{align*}
Next text
\begin{align*}
  a + b &= c\\
  d &= e + f
\end{align*}

@NoPrefInside option

There is something a bit painful in our definition of > for align environment above. For example, if we try to translate this code:

@Define
  @Prefs
    >  = @TexBeginEnd "align*" @Sep "\\\\" @Math

Long equation
  > a_1 + a_2 + a_3 + a_4
        + a_5 + a_6 + a_7

we'll get an error:

  |
7 |         + a_5 + a_6 + a_7
  |           ^
Unexpected prefix: +

If we want to use prefixes in align the only way to fix it is to place empty curly brackets before +:

...
  > a_1 + a_2 + a_3 + a_4
      {}+ a_5 + a_6 + a_7

But if we know, that there should be no prefixes in align environment we can inform translator about this adding @NoPrefInside option to definition:

@Define
  @Prefs
    >  = @TexBeginEnd "align*" @Sep "\\\\" @Math @NoPrefInside

Long equation
  > a_1 + a_2 + a_3 + a_4
        + a_5 + a_6 + a_7

Math commands

@Define
  @Environments
    Equation  = @TexBeginEnd "equation" @Math
    Equation' = @TexBeginEnd "equation"
  @Prefs
    > = @TexBeginEnd "align" @Sep "\\\\" @Math
    ] = @TexBeginEnd "align" @Sep "\\\\"
  @MathCommands
    <= = "\\geq"

First of all, in paragraphs. Math commands will be replaced in inline math only:
`a <= b` works, but this <= will not be replaced.

@Equation
  a^2 + b^2 <= c^2
@Equation'
  a^2 + b^2 <= c^2

And in prefs:
  > a^2 + b^2 <= c^2
  ] a^2 + b^2 <= c^2
First of all, in paragraphs. Math commands will be replaced in inline math only: $a \geq b$ works,
but this <= will not be replaced.

\begin{equation}
  a^2 + b^2 \geq c^2
\end{equation}
\begin{equation}
  a^2 + b^2 <= c^2
\end{equation}

And in prefs:
\begin{align}
  a^2 + b^2 \geq c^2
\end{align}
\begin{align}
  a^2 + b^2 <= c^2
\end{align}

Unicode symbols

@Define
  @MathCommands
    ∀ = "\\forall"
    ∃ = "\\exists"
    δ = "\\delta"
    ϵ = "\\epsilon"
    -> = "\\rightarrow"
    => = "\\Rightarrow"
  @Prefs
    > = @TexBeginEnd "align" @Sep "\\\\" @Math

The function `f` has a limit `y` when `x -> x_0` iff
  > ∀ϵ > 0 ∃δ > 0 : ∀x (|x - x_0| < δ => |f(x) - y| < ϵ)
The function $f$ has a limit $y$ when $x \rightarrow x_0$ iff
\begin{align}
  \forall\epsilon > 0 \exists\delta > 0 : \forall x (|x - x_0| < \delta \Rightarrow |f(x) - y| <
  \epsilon)
\end{align}