mdthereom 共享計數器、cleverref 和嵌套定理

mdthereom 共享計數器、cleverref 和嵌套定理

我在嘗試設定 mdframed 時遇到問題

  1. 它與 smartref 相容
  2. 所有 mdframed 盒子共享同一個計數器
  3. 嵌套 mdframed 環境時不會中斷

演示文件。理想情況下我們應該看到

  • 引理1.1:主要引理

  • 註1.2:計算

  • 引理1.3:計算1

  • 引理1.4:計算2

  • 備註1.5:rmk 1

這是我的原始序言文件,它完成了 1) 和 3)。但我們有引理 1.1、註 1.1、引理 1.2、引理 1.3。備註 1.1 因為每個計數器的計數器是獨立的\mdtheorem

\documentclass{book}
\usepackage{amsthm,xcolor,mdframed,hyperref,cleveref}

%Headers and Sections

% Colors

\definecolor{tab-blue}{rgb}{0.341,0.471,0.643}
\definecolor{tab-orange}{rgb}{0.894,0.580,0.267}

%%%
%Counters
\newcounter{thechapter}
%%%
% Define fChapter
%for putting chapter numbers after the title


% Chapters
\newcommand{\fchapter}[1]%
{%
    \phantomsection\chapter*{Chapter #1}%
    \stepcounter{thechapter}
    \newpage% Chapter Number
} 

% Copy the same style but modify the colour only
\mdfdefinestyle{theoremstyle}{%
    backgroundcolor=tab-blue!10
}

\mdfdefinestyle{remarkstyle}{%
    backgroundcolor=tab-blue!5,
}

\mdfdefinestyle{notestyle}{%
    backgroundcolor=tab-orange!5,
}

\mdtheorem[style=theoremstyle]{lemma}{Lemma}[thechapter]
\mdtheorem[style=remarkstyle]{remark}{Remark}[thechapter]
\mdtheorem[style=notestyle]{note}{Note}[thechapter]

% Here we manage Clever Headers
\crefname{lemma}{lem.}{lems.}
\Crefname{lemma}{Lemma}{Lemmas}

\crefname{note}{note}{notes}
\Crefname{note}{Note}{Notes}

\begin{document}
\fchapter{1: main result}
\begin{lemma}[main lemma]\label{lem:main}
1234556
\end{lemma}
\begin{proof}
    123
\begin{note}[calculations]
\begin{lemma}[calculation 1]\label{lem:calc 1}
calc 1 here
\end{lemma}
\begin{proof}
calc 1 proof
\end{proof}
\begin{lemma}[calculation 2]\label{lem:calc 2}
calc 2 here
\end{lemma}
\begin{proof}
calc 2 proof
\end{proof}
\begin{remark}[rmk 1]\label{rmk: rmk 1}
rmk 
\end{remark}
\end{note}

\end{proof}

Cleveref tests: 
\begin{itemize}
    \item main lemma \cref{lem:main}
    \item note calculations: \cref{note:calculations}
    \item lemma calc 1: \cref{lem:calc 1}
    \item lemma calc 2: \cref{lem:calc 2}
    \item rmk 1 1: \cref{rmk: rmk 1}
\end{itemize}
\end{document}


從中汲取靈感這裡,並使用新的計數器boxcounter,我替換了\mdtheorem上一個文件中的三行。我們現在有一個共享計數器,但我們破壞了 Cleveref(在嘗試引用環境時顯示 ???)。這是因為\cref試圖引用 boxcounter 而不是環境本身。

巢狀環境也被破壞,有重複的數字(註 1.5 應該是註 1.2),編譯文件給出

引理 1.1、註 1.5、引理 1.3、引理 1.4、備註 1.5。

\documentclass{book}
\usepackage{amsthm,xcolor,mdframed,hyperref,cleveref}
% Colors
\definecolor{tab-blue}{rgb}{0.341,0.471,0.643}
\definecolor{tab-orange}{rgb}{0.894,0.580,0.267}

%%%
%Counters
\newcounter{thechapter}

% Chapters
\newcommand{\fchapter}[1]%
{%
    \phantomsection\chapter*{Chapter #1}%
    \stepcounter{thechapter}
    \newpage% Chapter Number
} 

% Copy the same style but modify the colour only
\mdfdefinestyle{theoremstyle}{%
    backgroundcolor=tab-blue!10
}

\mdfdefinestyle{remarkstyle}{%
    backgroundcolor=tab-blue!5,
}

\mdfdefinestyle{notestyle}{%
    backgroundcolor=tab-orange!5,
}

\newcounter{boxcounter}[thechapter]
\renewcommand{\theboxcounter}{\arabic{thechapter}.\arabic{boxcounter}}

\newenvironment{lemma}[1][]{%
    \refstepcounter{boxcounter}
    \begin{mdframed}[%
        frametitle={Lemma \theboxcounter\ #1},
        style=theoremstyle
    ]%
}{%
    \end{mdframed}
}

\newenvironment{note}[1][]{%
    \refstepcounter{boxcounter}
    \begin{mdframed}[%
        frametitle={Note \theboxcounter\ #1},
        style=notestyle
    ]%
}{%
    \end{mdframed}
}

\newenvironment{remark}[1][]{%
    \refstepcounter{boxcounter}
    \begin{mdframed}[%
        frametitle={Remark \theboxcounter\ #1},
        style=remarkstyle
    ]%
}{%
    \end{mdframed}
}

% Here we manage Clever Headers
\crefname{lemma}{lem.}{lems.}
\Crefname{lemma}{Lemma}{Lemmas}

\crefname{note}{note}{notes}
\Crefname{note}{Note}{Notes}

\begin{document}
\fchapter{1: main result}
\begin{lemma}[main lemma]\label{lem:main}
1234556
\end{lemma}
\begin{proof}
    123
\begin{note}[calculations]\label{note:calculations}
\begin{lemma}[calculation 1]\label{lem:calc 1}
calc 1 here
\end{lemma}
\begin{proof}
calc 1 proof
\end{proof}
\begin{lemma}[calculation 2]\label{lem:calc 2}
calc 2 here
\end{lemma}
\begin{proof}
calc 2 proof
\end{proof}
\begin{remark}[rmk 1]\label{rmk: rmk 1}
rmk 
\end{remark}
\end{note}

\end{proof}

Cleveref tests: 
\begin{itemize}
    \item main lemma \cref{lem:main}
    \item note calculations: \cref{note:calculations}
    \item lemma calc 1: \cref{lem:calc 1}
    \item lemma calc 2: \cref{lem:calc 2}
    \item rmk 1 1: \cref{rmk: rmk 1}
\end{itemize}
\end{document}

答案1

由於您沒有發布可編譯的MWE,因此下面的答案並未直接針對您的需求,而僅用於說明原理。第三個顯示的程式碼區塊不起作用的原因是您在三個環境中使用相同的計數器。 Cleveref 使用計數器名稱來區分各種環境。

解決這個問題的方法(內部用於cleveref處理共享相同計數器的定理環境,amsthm該計數器創建的計數器具有相同的問題)是創建一個重複的從屬計數器。

例如

\documentclass{article}
\usepackage{cleveref}

\newcounter{mastercounter}
\newcounter{envAcount}
\newcounter{envBcount}
\makeatletter
\let\c@envAcount\c@mastercounter
\let\c@envBcount\c@mastercounter
\makeatother

\newenvironment{envA}{\refstepcounter{envAcount}\textbf{First Environment \theenvAcount}\par}{}
\newenvironment{envB}{\refstepcounter{envBcount}\textbf{Second Enviornment \theenvBcount}\par}{}

\Crefname{envAcount}{First}{First}
\Crefname{envBcount}{Second}{Second}

\begin{document}

\begin{envA}\label{envAtest}
        Test
\end{envA}

\begin{envB}\label{envBtest}
        Another test
\end{envB}

\Cref{envAtest} and \Cref{envBtest}

\end{document}
  1. 此程式碼建立三個計數器:mastercounterenvAcountenvBcount
  2. \makeatletter...\makeatothermakeenvAcount與內部計數器之間的行envBcount與 相同mastercounter,因此增加 1 會增加所有三個*
  3. 然後我們定義新環境envAenvB請注意,在每種環境中您\refstepcounter可以選擇envAcountenvBcount酌情使用。這很重要,因為.aux當您\label設定環境時,計數器名稱會記錄在檔案中。
  4. 請注意,我們定義\Crefname使用櫃檯名稱,而不是使用計數器的環境名稱。

上面的程式碼產生以下輸出 在此輸入影像描述

現在,您可以自行將該技巧合併到您的完整文件中,用於mdframed定義您的環境。

*手動執行此操作的另一種方法是使用類似的套件別名


現在您已經新增了 MWE,這裡有一個適合您的 MWE 的版本

\documentclass{book}
\usepackage{amsthm,xcolor,mdframed,hyperref,cleveref}
% Colors
\definecolor{tab-blue}{rgb}{0.341,0.471,0.643}
\definecolor{tab-orange}{rgb}{0.894,0.580,0.267}

%%%
%Counters
\newcounter{thechapter}

% Chapters
\newcommand{\fchapter}[1]%
{%
    \phantomsection\chapter*{Chapter #1}%
    \stepcounter{thechapter}
    \newpage% Chapter Number
} 

% Copy the same style but modify the colour only
\mdfdefinestyle{theoremstyle}{%
    backgroundcolor=tab-blue!10
}

\mdfdefinestyle{remarkstyle}{%
    backgroundcolor=tab-blue!5,
}

\mdfdefinestyle{notestyle}{%
    backgroundcolor=tab-orange!5,
}

\newcounter{boxcounter}[thechapter]
\renewcommand{\theboxcounter}{\arabic{thechapter}.\arabic{boxcounter}}
\newcounter{lemma}
\newcounter{note}
\newcounter{remark}
\makeatletter
\let\c@lemma\c@boxcounter
\let\c@note\c@boxcounter
\let\c@remark\c@boxcounter
\let\thelemma\theboxcounter
\let\thenote\theboxcounter
\let\theremark\theboxcounter
\makeatother


\newenvironment{lemma}[1][]{%
    \refstepcounter{lemma}
    \edef\x{Lemma \thelemma\ #1}
    \begin{mdframed}[%
        frametitle={\x},
        style=theoremstyle
    ]%
}{%
    \end{mdframed}
}

\newenvironment{note}[1][]{%
    \refstepcounter{note}
    \edef\x{Note \thenote\ #1}
    \begin{mdframed}[%
        frametitle={\x},
        style=notestyle
    ]%
}{%
    \end{mdframed}
}

\newenvironment{remark}[1][]{%
    \refstepcounter{remark}
    \edef\x{Remark \theremark\ #1}
    \begin{mdframed}[%
        frametitle={\x},
        style=remarkstyle
    ]%
}{%
    \end{mdframed}
}

% Here we manage Clever Headers
\crefname{lemma}{lem.}{lems.}
\Crefname{lemma}{Lemma}{Lemmas}

\crefname{note}{note}{notes}
\Crefname{note}{Note}{Notes}

\begin{document}
\fchapter{1: main result}
\begin{lemma}[main lemma]\label{lem:main}
1234556
\end{lemma}
\begin{proof}
    123
\begin{note}[calculations]\label{note:calculations}
\begin{lemma}[calculation 1]\label{lem:calc 1}
calc 1 here
\end{lemma}
\begin{proof}
calc 1 proof
\end{proof}
\begin{lemma}[calculation 2]\label{lem:calc 2}
calc 2 here
\end{lemma}
\begin{proof}
calc 2 proof
\end{proof}
\begin{remark}[rmk 1]\label{rmk: rmk 1}
rmk 
\end{remark}
\end{note}

\end{proof}

Cleveref tests: 
\begin{itemize}
    \item main lemma \cref{lem:main}
    \item note calculations: \cref{note:calculations}
    \item lemma calc 1: \cref{lem:calc 1}
    \item lemma calc 2: \cref{lem:calc 2}
    \item rmk 1 1: \cref{rmk: rmk 1}
\end{itemize}
\end{document}

這表明

在此輸入影像描述

這裡的一個技巧是這條線

\edef\x{Note \thenote\ #1}

解決這樣的問題:如果您投入\thenoteframetitle={Note \thenote\ #1}那麼當mdframed您開始擴展它時, 的值\thenote已經被內部環境改變了。這個技巧基本上確保儲存\thenoteinto的當前值\x,並且透過「變數範圍」的魔力,當內部環境退出時, 的值\x恢復到進入環境之前的情況。

相關內容