mdthereom 공유 카운터, Cleverref 및 중첩 정리

mdthereom 공유 카운터, Cleverref 및 중첩 정리

mdframed를 설정하는 데 문제가 있습니다.

  1. 그것은 Cleverref와 호환됩니다
  2. 모든 mdframed 상자는 동일한 카운터를 공유합니다.
  3. mdframed 환경이 중첩되어도 중단되지 않습니다.

데모 파일. 이상적으로는 우리가 봐야 할 것

  • 보조정리 1.1: 주요 보조정리

  • 참고 1.2: 계산

  • 정리 1.3: 계산 1

  • 정리 1.4: 계산 2

  • 비고 1.5: rmk 1

다음은 1)과 3)을 수행하는 원본 프리앰블 파일입니다. 하지만 Lemma 1.1, Note 1.1, Lemma 1.2, Lemma 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를 참조하려고 하기 때문입니다 .

문서를 컴파일하면 다음과 같이 반복되는 숫자(Note 1.5는 Note 1.2여야 함)로 인해 중첩된 환경도 깨집니다.

Lemma 1.1, Note 1.5, Lemma 1.3, Lemma 1.4, Remark 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. 이 코드는 , 및 의 세 가지 카운터를 mastercounter생성 envAcount합니다 envBcount.
  2. \makeatletter...\makeatothermake 사이의 행은 내부적으로 envAcountenvBcount동일한 카운터이므로 mastercounter하나를 늘리면 세 개가 모두 증가합니다 * .
  3. 그런 다음 새로운 환경을 정의 envA하고 envB; 각 환경에서 적절 \refstepcounter하게 수행 envAcount하십시오 envBcount. 환경을 .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}

\thenote를 넣었을 때 확장할 frametitle={Note \thenote\ #1}때쯤에 이미 실내 환경에 따라 mdframed의 가치가 \thenote변해 버린 문제를 해결하기 위해 . 이 트릭은 기본적으로 의 현재 값을 에 저장 \thenote하고 \x내부 환경이 종료될 때 "가변 범위 지정"의 마법을 사용하여 의 값이 \x환경에 들어가기 전의 경우로 복원됩니다.

관련 정보