如何在 LaTeX 中创建一个可运行的示例

如何在 LaTeX 中创建一个可运行的示例

我想要一个continues example在不同部分中使用相同数字的示例。LaTeX 是否可以不增加示例的计数器?

问候

最低工作代码:

\documentclass{article}

\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{amsmath}

\theoremstyle{definition}
\newtheorem{myexp}{Example}

\begin{document}

\begin{myexp}
This is the first part of our running example
\end{myexp}

.
.
.

\begin{myexp}
This is the second part of our running example
\end{myexp}


\end{document}

答案1

解决方案构想

我们在这里使用两个环境,原始环境myexp和另一个环境(实际上基于myexpmyexpcont,后者表示正在运行的示例的继续。

对于myexpcont环境,定理计数器保持不变(在开始前将其值降低 1)。我们还添加了文本 (继续) 以示延续。

代码

\documentclass{article}

\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{amsmath}

\theoremstyle{definition}
\newtheorem{myexp}{Example}

\newenvironment{myexpcont}
{\addtocounter{myexp}{-1}\begin{myexp}{\textit{\textbf{{(continued)}}}}}
  {\end{myexp}}

\begin{document}

\begin{myexp}
This is the first part of our running example.
\end{myexp}


\begin{myexpcont}
This is the second part of our running example.
\end{myexpcont}

\begin{myexp}
This is the first part of another example.
\end{myexp}

\begin{myexpcont}
This is the second part of our running example.
\end{myexpcont}

\begin{myexpcont}
This is the third part of our running example.
\end{myexpcont}

\begin{myexp}
This is the only part of another example.
\end{myexp}

\begin{myexp}
This is the only part of yet another example.
\end{myexp}

\end{document}

输出

在此处输入图片描述

答案2

thmtools软件包通过选项为此提供了一种直接机制continues。您用例如标记第一个实例\label{ex:first},然后在后续实例中提供选项[continues=ex:first]

continues您可以通过重新定义命令来更改选项打印的标准文本\thmcontinues

示例输出

\documentclass{article}

\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{thmtools}

\theoremstyle{definition}
\newtheorem{myexp}{Example}

\begin{document}

\begin{myexp}
  \label{ex:first}
  This is the first part of our running example.
\end{myexp}

Some text and interesting discussion.

\begin{myexp}[continues=ex:first]
  This is the second part of our running example.
\end{myexp}

More text and interesting discussion.

\renewcommand{\thmcontinues}[1]{continued}
\begin{myexp}[continues=ex:first]
  This is the third part of our running example.
\end{myexp}

\end{document}

的选项\thmcontiunes是传递给 的标签continues。标准文本通过大致相当于以下的定义生成对第一个实例页面的引用

\renewcommand{\thmcontiunes}[1]{continuing from p.\ \pageref{#1}}

(它实际上包含更多在加载时与包交互的代码hyperref。)

相关内容