1

I am looking for a convenient way to code nestable "contexts" whose extents are displayed through indentation. Each context consists of a sequence of "statements" (i.e. lines) and/or (1 level deeper) "children" contexts.

The desired results would look something like this:

This statement is in the "outermost" context.
Let's call this context 0.
  This statement is in the next context.
  Let's call it context 1.
  Statements are expected to be short.
    Behold: an even deeper context.
  OK, back to context 1.
  The number of statements in each context is arbitrary.
    So is the number of subcontexts in a context.
    But context level can change by at most 1 at a time.
  OK, let's finish this example...
We're almost done.
There!

As I'm thinking of it, the LaTeX source code for the above would look something like this:

\begin{context}
This statement is in the "outermost" context. \\
Let's call this context 0.
\begin{context}
This statement is in the next context. \\
Let's call it context 1. \\
Statements are expected to be short.
\begin{context}
Behold: an even deeper context.
\end{context}
OK, back to context 1. \\
The number of statements in each context is arbitrary.
\begin{context}
So is the number of subcontexts in a context. \\
But context level can change by at most 1 at a time.
\end{context}
OK, let's finish this example...
\end{context}
We're almost done. \\
There!
\end{context}

There are a couple of finer points regarding linebreaks and line wrapping. First, it would be great if a simple newline (rather than \\) were enough to indicate a linebreak, but somehow I doubt that this would be easy to do.

The second point refers to a feature that I did not know how to illustrate in the example above. It concerns "multiline" statements. Even though the typical statement will be short enough to not require any linebreaks, there may be rare instances (especially for deeply nested contexts) where a linebreak may be necessary, resulting in a multiline statement. Therefore, it would be good if the separation between individual statements were larger than the one between the lines in a multiline statement (but ideally just large enough to make the difference visually noticeable). Of course, for the occasional multiline statement, the leftmost endpoint of all its lines should line up vertically; i.e. all these lines should be left-justified, though at a common (possibly non-zero) indentation level.

Can someone show me how to define the \begin{context}/\end{context} sequence?

EDIT: Please see this math.stackexchange.com post, and in particular its Example section, for an illustration of the application I have in mind. (It turns out that that post includes some a custom extension to MathJax that the author uses to code its displays, but that code does not work for me when I use it in a regular LaTeX file.)

8

3 Answers 3

1

You can use a list, because this will accumulate the hanging indentation.

\documentclass{article}
\usepackage{showframe} % just to show the text box

\newlength{\contextindent}
\AtBeginDocument{\setlength{\contextindent}{2em}}
\newcounter{contextdepth}

\newenvironment{context}
 {%
  \addtocounter{contextdepth}{1}%
  \list{}{%
    \leftmargin=\ifnum\value{contextdepth}=1 0pt\else\contextindent\fi
    \topsep=0pt \partopsep=0pt \parsep=0pt
  }%
  \item\relax
 }
 {\endlist\addtocounter{contextdepth}{-1}}

\begin{document}

\begin{context}
This statement is in the "outermost" context. \\
Let's call this context 0.
\begin{context}
This statement is in the next context. \\
Let's call it context 1. \\
Statements are expected to be short.
\begin{context}
Behold: an even deeper context.
\end{context}
OK, back to context 1. \\
The number of statements in each context is arbitrary.
\begin{context}
So is the number of subcontexts in a context. \\
But context level can change by at most 1 at a time.
\end{context}
OK, let's finish this example...
\end{context}
We're almost done. \\
There!
\end{context}

\end{document}

Set \contextindent to your preferred value. I added the counter in order to distinguish when we're at “level 0”, so to not have a left margin.

enter image description here

2
  • Thank you! This is pretty much what I was looking for. Is there a way to control (a) the separation between statements; and (b) the separation between lines in (rare) multiline statements? (Here, by "multiline statements" I mean the (hopefully rare) statements that are long enough that text wrap-around becomes necessary.) I would like to ensure that the former is visibly greater than the latter.
    – kjo
    May 20, 2022 at 10:06
  • Also, I should add (in case it matters to the question in my previous comment) that the separation between all adjacent statements, even in cases where the two statements belong to different contexts, should be identical.
    – kjo
    May 20, 2022 at 10:09
2

My solution enables you not to use \\ at each end of line and long lines are wrapped as described by you.

\parindent=0pt
\newdimen\icontext \icontext=15pt

\def\context{\par \bgroup\advance\leftskip by\icontext
             \rightskip=0pt plus 1fil\relax \obeylines
             \everypar{\hangindent=2\icontext \hangafter=1 }}
\def\econtext{\par\egroup}

\hsize=10cm % for testing long lines

This statement is in the "outermost" context.\\
Let's call this context 0.
\context
   This statement is in the next context.
   Let's call it context 1.
   Statements are expected to be short.
   \context
      Behold: an even deeper context.
   \econtext
   OK, back to context 1.
   The number of statements in each context is arbitrary.
   \context
      So is the number of subcontexts in a context.
      But context level can change by at most 1 at a time. This line is very long long lon
   \econtext
   OK, let's finish this example...
\econtext
We're almost done.\\
There!
1
\documentclass{article}
\begin{document}
{\obeylines\parindent0pt
     This statement is in the "outermost" context.  
     Let's call this context 0.
\parindent2em
        This statement is in the next context.
        Let's call it context 1.
        Statements are expected to be short. 
\parindent4em
             Behold: an even deeper context. 
\parindent2em
        OK, back to context 1. 
        The number of statements in each context is arbitrary.
\parindent4em
            So is the number of subcontexts in a context. 
            But context level can change by at most 1 at a time.  
\parindent2em
        OK, let's finish this example...
\parindent0pt
 We're almost done. 
 There!}
\end{document}

or ...

\documentclass{article}
\begin{document}
\begin{itemize} \parskip0pt
\item[]  This statement is in the "outermost" context. 
\item[]  Let's call this context 0.
    \begin{itemize} 
    \item[]  This statement is in the next context.
    \item[]  Let's call it context 1.
    \item[]  Statements are expected to be short. 
        \begin{itemize}
        \item[]  Behold: an even deeper context.
        \end{itemize}
    \item[] OK, back to context 1.   
    \item[] The number of statements in each context is arbitrary.
        \begin{itemize}
        \item[] So is the number of subcontexts in a context. 
        \item[] But context level can change by at most 1 at a time.  
        \end{itemize}
        \item[] OK, let's finish this example...
    \end{itemize}
\item[]  We're almost done. \par  There!
\end{itemize}
\end{document}

You can use here a blank line (par) to break paragraphs of a item instead of use multiple items in the same context If you want a numerated contexts simple change every {itemze} by {enumerate} and \item[] by \item.

Note that standard itemize and enumerate environments can be nested to have 4 levels only but you can combine nest one type on the other to have more than four.

Another way for enumerated contexts:

\documentclass{article}
\usepackage{linguex}
\begin{document}
\ex. This statement is in the "outermost" context. \\ 
     Let's call this context 0.
    \a. This statement is in the next context.\\  
        Let's call it context 1. \\ 
        Statements are expected to be short. 
        \a.Behold: an even deeper context. \z. 
    \b. OK, back to context 1. \\
        The number of statements in each context is arbitrary.
        \a. So is the number of subcontexts in a context. \\
            But context level can change by at most 1 at a time.  \z.
        OK, let's finish this example...
\ex. We're almost done. \\ There!
\end{document}

Note that the number of levels of list of linguex is limited and that the blank lines (\par) also have a special meaning here (end level or stop the list) so you must use \\ to break ines whitin items. Beside the easy but odd syntax, another difference is that by default the first level counter is not reset after return to main text, so you can continue with the same list after that just starting again with \ex.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .