我正在寻找一种方便的方式来编写可嵌套的“上下文”,其范围通过缩进显示。每个上下文由一系列“语句”(即行)和/或(深 1 级)“子”上下文组成。
期望的结果看起来应该是这样的:
此语句位于“最外层”上下文中。
我们将其称为上下文 0。
此语句位于下一个上下文中。
我们将其称为上下文 1。
语句应该很短。
请注意:一个更深的上下文。
好的,回到上下文 1。
每个上下文中的语句数量是任意的。
上下文中的子上下文数量也是如此
。但上下文级别每次最多可以改变 1。
好的,让我们完成这个例子……
我们快完成了。
好了!
正如我所想,上述内容的 LaTeX 源代码看起来应该是这样的:
\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}
关于换行和换行,还有几点需要注意。首先,如果一个简单的换行符(而不是\\
)就足以指示换行符,那就太好了,但不知何故,我怀疑这是否容易做到。
第二点指的是我不知道如何在上面的例子中说明的一个特性。它涉及“多行”语句。尽管典型的语句足够短,不需要任何换行符,但可能存在极少数情况(尤其是对于深度嵌套的上下文),可能需要换行符,从而产生多行语句。因此,如果单个语句之间的间隔大于多行语句中行之间的间隔(但理想情况下,大到足以使差异在视觉上显而易见),那就太好了。当然,对于偶尔出现的多行语句,其所有行的最左端点都应垂直对齐;即所有这些行都应该左对齐,尽管缩进级别是共同的(可能非零)。
有人可以告诉我如何定义\begin{context}
/\end{context}
序列吗?
编辑:请参见这篇 math.stackexchange.com 帖子,尤其是其例子部分,以说明我所想到的应用程序。(事实证明,该帖子包含一些 MathJax 的自定义扩展,作者使用它来编码其显示,但当我在常规 LaTeX 文件中使用它时,该代码对我不起作用。)
答案1
您可以使用list
,因为这将累积悬挂缩进。
\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}
设置\contextindent
为您喜欢的值。我添加了计数器,以便区分我们处于“级别 0”的情况,这样就不会有左边距。
答案2
\\
我的解决方案使您不必在行的每一端使用,并且长行将按照您描述的方式进行换行。
\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!
答案3
\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}
或者 ...
\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}
您可以在此处使用空白行 (par) 来分隔项目的段落,而不是在同一个上下文中使用多个项目如果您想要一个数字上下文,只需将每个{itemze}
by {enumerate} 和\item[]
by进行简单的更改\item
。
请注意,标准itemize
和enumerate
环境可以嵌套仅具有 4 个级别,但您可以将一种类型嵌套在另一种类型上,以具有超过四个级别。
枚举上下文的另一种方法:
\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}
请注意,linguex 列表的级别数量是有限的,并且空行 ( \par
) 在这里也有特殊含义(结束级别或停止列表),因此您必须使用\\
来中断列表中的项目。除了简单但奇怪的语法之外,另一个区别是,默认情况下,返回主文本后不会重置第一级计数器,因此您可以继续使用相同的列表,只需再次开始\ex.