设置 Theorem 的“属性”以便稍后访问

设置 Theorem 的“属性”以便稍后访问

我正在写一篇包含大量定理的数学论文。我希望有一个表格来总结我的结果,如果表格可以从我的论文中自动生成,我会很高兴。我希望能够做这样的事情

\begin{thm}
 \shortTheorem{$x + y = y + x$}
 Let $x,y$ be integers. Then $x+y=y+x$.
 \end{thm}

请注意,shortTheorem 只是我为将所需文本放入的标签所起的名称。

然后,稍后我想要一个表,对于我定义的每个定理,打印其“shortTheorem”内容。即

  • x + y = y + x

是否有现有的软件包可以执行此操作?如果没有,我该如何编写自己的宏来执行此操作?

(注意:我无法弄清楚是否有办法在我的问题中实际呈现 LaTeX 表,如果您知道如何操作,请随意编辑。)

答案1

这会在最后生成一个表格,我想这就是你想要的:

在此处输入图片描述

\documentclass{article}

\makeatletter
\def\shortthms{}
\def\shorttheorem#1{\g@addto@macro\shortthms{#1\\}}
\makeatother

\newtheorem{thm}{Theorem}

\begin{document}



\begin{thm}
 \shorttheorem{$x + y = y + x$}
  Let $x,y$ be integers. Then $x+y=y+x$.
 \end{thm}

\begin{thm}
 \shorttheorem{$x + x = x + x$}
 Let $x,y$ be integers. Then $x+x=x+x$.
 \end{thm}


blab blah

\begin{tabular}{p{5cm}}
\shortthms
\end{tabular}

\end{document}

计划 b,使用文件需要两次 Latex 运行来提取数据:

\documentclass{article}

\makeatletter
%\def\shortthms{}
%\def\shorttheorem#1{\g@addto@macro\shortthms{#1\\}}

\def\shorttheorem#1{\immediate\write\shortthmout{\unexpanded{#1\\}}}


\def\shortthms{%
\begin{tabular}{p{5cm}}\@input{\jobname.thm}\end{tabular}%
\newwrite\shortthmout
\immediate\openout\shortthmout=\jobname.thm}

\makeatother

\newtheorem{thm}{Theorem}

\begin{document}


\shortthms



\begin{thm}
 \shorttheorem{$x + y = y + x$}
  Let $x,y$ be integers. Then $x+y=y+x$.
 \end{thm}

\begin{thm}
 \shorttheorem{$x + x = x + x$}
 Let $x,y$ be integers. Then $x+x=x+x$.
 \end{thm}


blab blah


\end{document}

答案2

如果您还想打印定理编号,这里有一个示意性解决方案:

\documentclass{article}
\usepackage{lipsum}% just for the example

\newcommand{\shorttheorem}[1]{%
  \xdef\shorttheoremlist{%
    \unexpanded\expandafter{\shorttheoremlist}%
    \noexpand\item[Theorem \thethm:]
    \unexpanded{#1}}}

\newcommand\shorttheoremlist{}
\newcommand\printshorttheorems{%
  \begin{description}
  \shorttheoremlist
  \end{description}}

\newtheorem{thm}{Theorem}

\begin{document}

\section{A section}

\lipsum[2]

\begin{thm}\shorttheorem{$x+y=y+x$}
Let $x$ and $y$ be integers. Then $x+y=y+x$.
\end{thm}

\lipsum[3]

\begin{thm}\shorttheorem{$\sqrt{x^2}=|x|$}
Let $x$ be an integer. Then $\sqrt{x^2}=|x|$.
\end{thm}

\section{The final list}

\printshorttheorems

\end{document}

在此处输入图片描述

扩大的视野

如果你想用不同的方式命名定理并将它们放在最终列表中,那么cleveref就可以解决这个问题。加载amsthmntheorem(我更喜欢前者)并定义类似定理的环境很重要正在加载cleveref。另外,请确保\shorttheorem在环境开始时添加位。

\documentclass{article}
\usepackage{lipsum}% just for the example

\usepackage{amsthm}
\usepackage{cleveref}

\newcommand{\shorttheorem}[1]{%
  \stepcounter{shortcount}%
  \label{shortcount\theshortcount}%
  \xdef\shorttheoremlist{%
    \unexpanded\expandafter{\shorttheoremlist}%
    \noexpand\item[\noexpand\Cref{shortcount\theshortcount}:]
    \unexpanded{#1}}}
\newcounter{shortcount}
\newcommand\shorttheoremlist{}
\newcommand\printshorttheorems{%
  \begin{description}
  \shorttheoremlist
  \end{description}}

\newtheorem{thm}{Theorem}
\newtheorem{lem}[thm]{Lemma}

\begin{document}

\section{A section}

\lipsum[2]

\begin{lem}\shorttheorem{$x+y=y+x$}
Let $x$ and $y$ be integers. Then $x+y=y+x$.
\end{lem}

\lipsum[3]

\begin{thm}\shorttheorem{$\sqrt{x^2}=|x|$}
Let $x$ be an integer. Then $\sqrt{x^2}=|x|$.
\end{thm}

\section{The final list}

\printshorttheorems

\end{document}

在此处输入图片描述

扩展版本,可在文档中任意放置

\documentclass{article}
\usepackage{lipsum}% just for the example

\usepackage{amsthm}
\usepackage{cleveref}

\makeatletter
\newwrite\shorttheoremfile
\newcounter{shortcount}
\newcommand{\shorttheorem}[1]{%
  \stepcounter{shortcount}%
  \label{shortcount\theshortcount}%
  \xdef\shorttheoremlist{%
    \unexpanded\expandafter{\shorttheoremlist}%
    \string\item[\string\Cref{shortcount\theshortcount}:]
    \unexpanded{#1}^^J}%
}
\newcommand{\shorttheoremlist}{}
\AtEndDocument{
  \immediate\openout\shorttheoremfile=\jobname.thm
  \immediate\write\shorttheoremfile{\unexpanded\expandafter{\shorttheoremlist}}
  \immediate\closeout\shorttheoremfile
}

\newcommand\printshorttheorems{%
  \begin{description}
  \def\@noitemerr{\@latex@warning{Empty short theorem list}}%
  \@input{\jobname.thm}
  \end{description}
}
\makeatother

\newtheorem{thm}{Theorem}
\newtheorem{lem}[thm]{Lemma}

\begin{document}

\section{Short theorems}

\printshorttheorems

\section{A section}

\lipsum[2]

\begin{lem}\shorttheorem{$x+y=y+x$}
Let $x$ and $y$ be integers. Then $x+y=y+x$.
\end{lem}

\lipsum[3]

\begin{thm}\shorttheorem{$\sqrt{x^2}=|x|$}
Let $x$ be an integer. Then $\sqrt{x^2}=|x|$.
\end{thm}

\end{document}

相关内容