带有超链接的文章简化分段

带有超链接的文章简化分段

对于我的文章,我希望使用一种非常简单的通用编号方法,其中包含两个计数器,一个用于除方程式之外的任何内容(无论是定义、引理等),另一个用于方程式。这两个计数器大多数时候共享相同的值,除非非方程式项包含一个或多个方程式(在这种情况下,方程计数器会重新初始化,然后方程标签会同时包含项的索引和项中方程式列表内的方程式索引)。

另外,我希望对 \theoremstyle{change} 行为进行修改,即在计数器和项目名称之间插入一个句点。换句话说,我想要一个由以下快照总结的行为:

图片

这是我的失败尝试:

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage{theorem}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
}
\usepackage{color}

\theoremstyle{change}
\newtheorem{defn}{Definition}
\newtheorem{fact}[defn]{Fact}
\newtheorem{thrm}[defn]{Theorem}


\begin{document}

Hello world, I am a short article. I start with a definition : 
\begin{defn}\label{firstdef} Blah blah blah.
\end{defn}

This definition \ref{firstdef} just made is followed by a fact : 

\begin{fact} \label{firstfact} Bluh bluh bluh.
\end{fact}

The fact \ref{firstfact} above is followed by a standalone equation : 

\begin{equation} \label{abequation} a=b
\end{equation}

This equation \ref{abequation} is followed by a theorem containing two equations : 

Followed by a theorem containing two equations : 

\begin{thrm} \label{mainthrm}The following two equations hold : 
\begin{equation} \label{firsteqnintheorem} c=d \end{equation}
\begin{equation} \label{secondeqnintheorem} e=f \end{equation}
\end{thrm}

Notice the different labeling of the equations \ref{firsteqnintheorem} and \ref{secondeqnintheorem}
inside theorem \ref{mainthrm}.
\end{document}

答案1

我相信有更好的方法来实现您的目标,但至少在您提供的示例中,以下方法有效:

\documentclass[a4paper,12pt]{article}
\usepackage{etoolbox,ifthen}
\usepackage{amsmath}
\usepackage{theorem}
\usepackage{hyperref}
\hypersetup{
   colorlinks=true,
   linkcolor=blue,
}
\usepackage{color}

\theoremstyle{change}
\newtheorem{defnHelper}[equation]{Definition} % These environments will only be used internally, never call \begin{defnHelper}...\end{defnHelper}!
\newtheorem{factHelper}[equation]{Fact}
\newtheorem{thrmHelper}[equation]{Theorem}
% This is the defn-environment you will call.
\newenvironment{defn}[1][]{ % Therefore, we define a new environment.
   \ifthenelse{ % (Only needed if you want to use the optional argument of your defn-environment.)
      \equal{}{#1} % If optional argument is empty (default), ...
   }{
      \begin{defnHelper} % ... just call defnHelper, ...
   }{
      \begin{defnHelper}[#1] % ... otherwise call defnHelper with optional argument.
   }
   \addtocounter{equation}{-1} % Make sure that the contained subequations have the same number as the containing environment.
   \begin{subequations}
}{
   \end{subequations}
   \end{defnHelper}
}
\newenvironment{fact}[1][]{ % The same for facts.
   \ifthenelse{
      \equal{}{#1}
   }{
      \begin{factHelper}
   }{
      \begin{factHelper}[#1]
   }
   \addtocounter{equation}{-1}
   \begin{subequations}
}{
   \end{subequations}
   \end{factHelper}
}
\newenvironment{thrm}[1][]{ % The same for theorems.
   \ifthenelse{
      \equal{}{#1}
   }{
      \begin{thrmHelper}
   }{
      \begin{thrmHelper}[#1]
   }
   \addtocounter{equation}{-1}
   \begin{subequations}
}{
   \end{subequations}
   \end{thrmHelper}
}

\makeatletter % As we use hyperref-package, we can adapt the subequations-numbering like that (thanks to https://tex.stackexchange.com/a/520531/237192).
\AtBeginDocument
{%
   \patchcmd\HyOrg@subequations
   {\alph}{.\arabic}{}{\GenericError{}{Patching failed}{}{}}%
}
\makeatother


\begin{document}
   
   Hello world, I am a short article. I start with a definition: 
   \begin{defn}\label{firstdef} Blah blah blah.
   \end{defn}
   
   This definition~\ref{firstdef} just made is followed by a fact: 
   
   \begin{fact} \label{firstfact} Bluh bluh bluh.
   \end{fact}
   
   The fact~\ref{firstfact} above is followed by a standalone equation: 
   
   \begin{equation} \label{abequation} a=b
   \end{equation}
   
   This equation~\ref{abequation} is followed by a theorem containing two equations: 
   
   Followed by a theorem containing two equations: 
   
   \begin{thrm} \label{mainthrm}The following two equations hold: 
      \begin{equation} \label{firsteqnintheorem} c=d \end{equation}
      \begin{equation} \label{secondeqnintheorem} e=f \end{equation}
   \end{thrm}
   
   Notice the different labeling of the equations~\ref{firsteqnintheorem} and \ref{secondeqnintheorem}
   inside theorem~\ref{mainthrm}.
\end{document}

在此处输入图片描述 一些说明:

  • 通常您应该使用see theorem~\ref{citedTheorem}以避免环境名称和其编号之间出现换行符。
  • 如果想要使用see theorem (4)Instead of see theorem 4,则使用\eqref{citedLabel}Instead of \ref{citedLabel}
  • 到目前为止,我还没有解决定理头的问题(你想要4. Theorem.而不是4 Theorem)。我将使用 -package 添加解决方案,amstheorem因为我对它更熟悉。见下文。

编辑:正如我所承诺的,这里是定理头问题的解决方案。替换\usepackage{theorem}\usepackage{amsthm}添加以下代码:

\newtheoremstyle{myStyle}%name
   {3pt}%                 Space above
   {3pt}%                 Space below
   {}%                    Body font
   {}%                    Indent amount
   {\bfseries}%           Theorem head font
   {.}%                   Punctuation after theorem head
   {.5em}%                Space after theorem head
   {\thmnumber{#2}.~\thmname{#1}\ifthenelse{\equal{}{#3}}{}{~{\normalfont(\thmnote{#3})}}}% Theorem head spec (can be left empty, meaning `normal')
\theoremstyle{myStyle}

您将需要进行一些尝试,尤其是上下空间。出于测试原因,我[test]向事实环境添加了一个可选参数。如果您想要另一种风格,只需尝试最后一个参数即可\newtheoremstyle

\begin{fact}[test] \label{firstfact} Bluh bluh bluh.

在此处输入图片描述 请注意,由于文档中有空行,因此所有行(定义、事实或定理开始的行除外)均缩进。(我指出这一点是为了让任何人都不会认为定理编号放在了页边空白处。)

相关内容