我使用 amsthm 包来定义引理、定理等的环境。它们都共享相同的计数器。\newcommand
我使用宏为计算问题(名称、输入、输出)定义了一个宏。我希望问题与引理、定理等共享相同的计数器。我该如何引用它?
目前我有这个:
\usepackage{amsthm}
\newtheorem{defn}{Definition}[chapter]
\newtheorem{lem}[defn]{Lemma}
\newcounter{problemdef}
\def\problemdefcnt{\thechapter.\arabic{problemdef}}
\newcommand{\problemdef}[3]{
\vspace{10pt} \noindent \textbf{Problem \problemdefcnt} #1 \\
\begin{tabular}{l | l}
\textbf{Input} & #2 \\
\textbf{Output} & #3
\end{tabular}
}
那么,我如何恰当地定义问题缺陷以获得类似
定义1.1
引理 1.2
问题 1.3
答案1
该计数器被命名为defn
,以便您可以轻松使用它。
\documentclass{book}
\usepackage{amsthm}
\newtheorem{defn}{Definition}[chapter]
\newtheorem{lem}[defn]{Lemma}
\newcommand{\problemdef}[3]{%
\par\vspace{10pt}
\noindent\refstepcounter{defn}\textbf{Problem \thedefn} #1\\*[1ex]
\begin{tabular}{l | l}
\textbf{Input} & #2 \\
\textbf{Output} & #3
\end{tabular}%
\par
}
\begin{document}
\chapter{Title}
\begin{defn}
A definition
\end{defn}
\begin{lem}
A lemma
\end{lem}
\problemdef{Whatever}{1}{10}
\end{document}
答案2
这个版本可能可以满足您的要求。(请注意,垂直间距可能不是最理想的,但我缺乏改进它的经验。)
\documentclass{report}
\usepackage{amsthm}
\newcounter{problemdef}[chapter]
\renewcommand{\theproblemdef}{\thechapter.\arabic{problemdef}}
\newtheorem{defn}[problemdef]{Definition}
\newtheorem{lem}[problemdef]{Lemma}
\newcommand{\problemdef}[3]{
\par\bigskip\noindent\textbf{Problem \refstepcounter{problemdef}\theproblemdef} #1
\par\medskip\noindent%
\begin{tabular}{l | l}
\textbf{Input} & #2 \\
\textbf{Output} & #3
\end{tabular}
\bigskip\par
}
\begin{document}
\chapter{A Chapter}
\begin{defn}
A (possibly non-hangleflip) groodle is \emph{normal} if it satisfies the
logos property for all $x > 17$.
\end{defn}
\begin{lem}
All normal groodles are hangleflip.
\end{lem}
\problemdef{Factorials}{an integer $n$}{$n!$}
\chapter{Another chapter}
\problemdef{Instantiation}{a name of a class}{an instance of that class}
\begin{defn}
A normal groodle is \emph{quasicoherent} if, locally, it admits a presentation (which
need not be finitely generated).
\end{defn}
\end{document}