1

All the ways I see to align things in math mode have you set columns, but I only care about where a line of math starts. I want to be able to write something like:

f:A\to \mathbb{R}\\       % indent 0
if \all x_0\in A\\        % indent 0
  \all \varepsilon>0\\    % indent 2
    \exists \delta s.t.\\ % indent 4
      x\in A\\            % indent 6
      if |x-x_0|<\delta\\ % indent 6
       \to|f(x)-f(x_0)|<\varepsilon\\ % indent 7
 \to f \text{continuous [cont.]}      % indent 1

The multiline environment allows me to do multiple lines, but I cannot indent without defining columns which is not what I want (the end of the line should go arbitrarily far). Bonus points if the indentation level defaults to that of the previous line.

zacchaeus
  • 13
  • 2
  • Do you want this entire construction centred or aligned to the left of the text block? Should it be breakable across the page boundary? – Werner May 20 '20 at 18:40
  • I had originally intended to left align it, but it would be nice to center it. If a line is too long, I expected to manually break the line up and indent the trailing line, but if you have something fancy in mind to automate the splitting of lines that go off the page, do tell. – zacchaeus May 20 '20 at 19:48
  • There are various algorithm packages available, although I don't know if they support your particular variety of pseudocode. (I've always wondered what function pseudocode satisfies, since it isn't standardized and is therefore subject to interpretation.) – John Kormylo May 21 '20 at 13:47

3 Answers3

2

You can define your own fancyvrb environment, similar to what is suggested in Environment that obeys spaces:

enter image description here

\documentclass{article}

\usepackage{amsmath,amsfonts,fancyvrb}

\let\all\forall% Whatever your \all implies

\DefineVerbatimEnvironment
  {MathIndent}
  {Verbatim}
  {fontfamily=cmr,
   commandchars=\\\{\},
  codes={\catcode`$=3\catcode`^=7\catcode`_=8}
  }

\begin{document}

\begin{MathIndent}
$f \colon A \to \mathbb{R}$
if $\all x_0 \in A$
  $\all \varepsilon > 0$
    $\exists \delta \text{s.t.}$
    $x \in A$
      if $\lvert x - x_0 \rvert < \delta$
       $\to \lvert f(x) - f(x_0) \rvert < \varepsilon$
 $\to f \text{continuous [cont.]}$
\end{MathIndent}

\end{document}

The tabbing environment allows similar alignments setting (of "tab stops") via \= and jumping/skipping via \>:

enter image description here

\documentclass{article}

\usepackage{amsmath,amsfonts}

\newcommand{\mindent}{\hspace{2em}}
\let\all\forall

\begin{document}

\begin{tabbing}
\= \mindent \= \mindent \= \mindent \= \kill                   \\
$f \colon A \to \mathbb{R}$                                    \\ % indent 0
if $\all x_0 \in A$                                            \\ % indent 0
\> $\all \varepsilon > 0$                                      \\ % indent 2
\> \> $\exists \delta \text{s.t.}$                             \\ % indent 4
\> \> \> $x \in A$                                             \\ % indent 6
\> \> \> if $\lvert x - x_0 \rvert < \delta$                   \\ % indent 6
\> \> \> \quad $\to \lvert f(x) - f(x_0) \rvert < \varepsilon$ \\ % indent 7
\quad $\to f \text{continuous [cont.]}$                           % indent 1
\end{tabbing}

\end{document}
Werner
  • 603,163
1

You can do that with the fleqn environment from nccmath,an align* environment and the optional argument of MoveEqLeft command from mathtools; the implicit unit of this command is em:

\documentclass{article}
\usepackage{amsfonts, nccmath}
\usepackage{mathtools}
\usepackage[showframe]{geometry}

\begin{document}

\begin{fleqn}%
    \begin{align*}
     & f:A\to \mathbb{R}\\ % indent 0
     &\text{if }\forall x_0\in A\\ % indent 0
    \MoveEqLeft[-2] \forall \varepsilon>0\\ % indent 2
    \MoveEqLeft[-4] \exists\, \delta \text{ s. t.} \\ % indent 4
    \MoveEqLeft[-6] x \in A\\ % indent 6
    \MoveEqLeft[-6]\text{if }|x-x_0|<\delta \\ % indent 6
    \MoveEqLeft[-7] \to|f(x)-f(x_0)|<\varepsilon\\ % indent 7
    [![enter image description here][1]][1]\MoveEqLeft[-1]\to f \text{ continuous [cont.]} % indent 1
    \end{align*}
\end{fleqn}

\end{document} 

enter image description here

Bernard
  • 271,350
1

The question was, what is the easiest way to do it. Maybe more easy than to write two $ at each line is the following solution.

{\catcode`\ =13 \gdef\dospace#1{\ifx#1 \hskip1em\else\catcode`\ =13\relax#1\fi}
\gdef\runindent{\bgroup\def\If{{\rm if}\ }
\def\par{\hfil\break\null\catcode`\ =13\relax}\obeylines   
\catcode`\ =13\def {\futurelet\next\dospace}\noindent$}}
\def\endindent{$\egroup}

\runindent
f \colon A \to R
\If \forall x_0 \in A
  \forall \varepsilon > 0
    \exists \delta {\rm s.t.}
    x \in A
      \If |x - x_0| < \delta
       \to |f(x) - f(x_0)| < \varepsilon
 \to f \hbox{continuous [cont.]}
\endindent
wipet
  • 74,238
  • OK, this looks perfect, but I cant really follow the first block you have. I tried to compile this with the first block before and after \begin{document}, but either way I get an error: "Style option: `fancyvrb' v3.3 <2019/10/22> (tvz) (/usr/share/texmf-dist/tex/latex/graphics/keyval.sty)) (./temp.aux) ! Extra else. is@range ...->if >#2expandafter check@single else expandafter check@range fi l.24 \runindent" – zacchaeus May 20 '20 at 20:21
  • @zacchaeus I added \bye at the end ad I compiled this by pdftex document without errors. – wipet May 20 '20 at 20:26
  • @zacchaeus OK, my mistake. I can't re-define \if. So, \If is defined here. See my new version of the answer. – wipet May 20 '20 at 20:40
  • This answers the question I asked PERFECTLY (I did not specify TeX or LaTeX), but I need it to work in a LaTeX format, and my LaTeX-foo isn't good enough to adapt this to a LaTeX format (pdftex -> pdflatex). – zacchaeus May 20 '20 at 20:42
  • @zacchaeus: See this setup to use it in LaTeX. – Werner May 20 '20 at 20:59
  • That worked! thanks – zacchaeus May 20 '20 at 21:13