语句缩进的版本,代码改编自https://tex.stackexchange.com/a/236407/4427

语句缩进的版本,代码改编自https://tex.stackexchange.com/a/236407/4427

梅威瑟:

\documentclass{book}
\usepackage{amsthm}
\usepackage{enumitem}
\usepackage{xparse}
\usepackage{hyperref}

\usepackage{lipsum}

\newtheoremstyle{fctaylor}% name
  {\topsep}%      Space above
  {\topsep}%      Space below
  {\normalfont}%         Body font
  {}%         Indent amount (empty = no indent, \parindent = para indent)
  {\bfseries}% Thm head font
  {}%        Punctuation after thm head
  {0pt}%     Space after thm head: " " = normal interword space;
  {\makethmhead{#1}{#2}{#3}}

\newlength\fctaylortheoremindent
\AtBeginDocument{\setlength\fctaylortheoremindent{3em}} % <- customize here
\newlength\fctaylorlabelsep
\AtBeginDocument{\setlength\fctaylorlabelsep{1em}} % <- customize here

\makeatletter
\newcommand{\makethmhead}[3]{%
  \gdef\thisthmhead{%
    \makebox[\fctaylortheoremindent][l]{\bfseries#2}%
    {\bfseries#1}%
    \@ifnotempty{#3}{ (#3)}%
    \hspace{\fctaylorlabelsep}%
    \phantomsection%%% THIS LINE ADDED
  }%
}
\makeatother

\newenvironment{fctayloritemize}
 {\list{}{%
    \leftmargin=\fctaylortheoremindent
    \labelwidth=\dimexpr\fctaylortheoremindent-\labelsep\relax
    \itemindent=0pt
  }}
 {\endlist}

\NewDocumentCommand{\newfctaylortheorem}{smomo}{%
  \IfBooleanTF{#1}
   {\newtheorem*{fctaylor@#2}{#4}}
   {\IfNoValueTF{#3}
     {\IfNoValueTF{#5}
       {\newtheorem{fctaylor@#2}{#4}}
       {\newtheorem{fctaylor@#2}{#4}[#5]}}
     {\newtheorem{fctaylor@#2}[fctaylor@#3]{#4}}}%
  \NewDocumentEnvironment{#2}{o}
   {\IfNoValueTF{##1}{\begin{fctaylor@#2}}{\begin{fctaylor@#2}[##1]}%
    \begin{fctayloritemize}\item[\thisthmhead\hfill]}
   {\end{fctayloritemize}\end{fctaylor@#2}}%
}

\theoremstyle{fctaylor}
\newfctaylortheorem{mytheorem}{Theorem}[chapter]
\newcommand{\thistheoremname}{}
\newfctaylortheorem{genericthm}[mytheorem]{\thistheoremname}
\newenvironment{custom}[1][Custom]
  {\renewcommand{\thistheoremname}{#1}%
   \begin{genericthm}}
  {\end{genericthm}}
\newfctaylortheorem*{genericthm*}{\thistheoremname}
\newenvironment{custom*}[1][Custom]
  {\renewcommand{\thistheoremname}{#1}%
   \begin{genericthm*}}
  {\end{genericthm*}}

\begin{document}
\section{One}

\begin{custom}{Custom Title}[optional words]\label{A}
\lipsum*[2]
\end{custom}

\newpage

\begin{custom}\label{B}
Even though I've been teaching for n! years, every class is a fresh
adventure -- An adventure. That word is a euphemism for the stark reality,
which is that every class contains some totally shocking development
that I have never seen before and haven't any idea how to cope with.

Just last year, for the first time in many years, I taught a section
of the junior-level mathematical analysis course. You know --  the one
where students meet proofs in analysis for the first time. The one where
students and epsilons meet, eyeball to eyeball, and it isn't the epsilons
that blink. The one where students decide that they really wanted to be
doctors and lawyers after all.
\end{custom}

\newpage

Here are the references: \ref{A} and \ref{B}.

\end{document}

我正在尝试模仿这个答案通过使自定义定理标题和可选参数成为可能,但我想设置可选参数的默认值。这样做时,我得到以下结果:

在此处输入图片描述

如果我尝试删除默认值,则会收到错误消息;! Argument of \label has an extra }.如果我不使用标签,则会收到此错误消息:

在此处输入图片描述

有没有简单的方法可以解决这个问题?可能不相关,但我听说我可以更轻松地使用\swapnumbers但这与我试图根除的行为有什么关系吗?(注意:我的定理环境来自这个答案

期望/预期输出:

\begin{custom}{Custom Title}[optional words]\label{A}
\lipsum*[2]
\end{custom}

会产生

在此处输入图片描述

\begin{custom}\label{B}
...
\end{custom}

会产生

在此处输入图片描述

答案1

可能使用G说明符,它表示括号中的可选参数:

\documentclass{article}
\usepackage{amsthm,xparse}

\usepackage{lipsum}

\makeatletter
\newtheoremstyle{jessica}% name
  {\topsep}%      Space above
  {\topsep}%      Space below
  {\normalfont}%         Body font
  {}%         Indent amount (empty = no indent, \parindent = para indent)
  {\bfseries}% Thm head font
  {}%        Punctuation after thm head
  {0pt}%     Space after thm head: " " = normal interword space;
  {%
   \makebox[0pt][r]{\makebox[\jessicaindent][l]{#2}}%
   #1%
   \@ifnotempty{#3}{\textnormal{ (#3)}}%    
   \hspace{\jessicalabelsep}%
  }
\makeatother

\newlength{\jessicaindent}
\newlength{\jessicalabelsep}
\setlength{\jessicaindent}{4em}
\setlength{\jessicalabelsep}{0.5em}

\theoremstyle{jessica}
\newtheorem{jessicacustom}{\jessicacustomname}[section]
\newtheorem*{jessicacustomnonumber}{\jessicacustomname}

\NewDocumentEnvironment{custom}{G{Custom}}
 {\def\jessicacustomname{#1}\jessicacustom}
 {\endjessicacustom}

\NewDocumentEnvironment{custom*}{G{Custom}}
 {\def\jessicacustomname{#1}\jessicacustomnonumber}
 {\endjessicacustomnonumber}

\begin{document}

\section{One}

\lipsum[4]

\begin{custom}{Custom Title}[optional words]\label{A}
\lipsum*[2]
\end{custom}

\begin{custom}\label{B}
Even though I've been teaching for n! years, every class is a fresh
adventure -- An adventure. That word is a euphemism for the stark reality,
which is that every class contains some totally shocking development
that I have never seen before and haven't any idea how to cope with.

Just last year, for the first time in many years, I taught a section
of the junior-level mathematical analysis course. You know --  the one
where students meet proofs in analysis for the first time. The one where
students and epsilons meet, eyeball to eyeball, and it isn't the epsilons
that blink. The one where students decide that they really wanted to be
doctors and lawyers after all.
\end{custom}

\begin{custom*}{Notnumbered}[xyz]
This is not numbered.
\end{custom*}

Here are the references: \ref{A} and \ref{B}.

\end{document}

在此处输入图片描述

我更喜欢键值接口:

\documentclass{article}
\usepackage{amsthm,xparse}

\usepackage{lipsum}

\makeatletter
\newtheoremstyle{jessica}% name
  {\topsep}%      Space above
  {\topsep}%      Space below
  {\normalfont}%         Body font
  {}%         Indent amount (empty = no indent, \parindent = para indent)
  {\bfseries}% Thm head font
  {}%        Punctuation after thm head
  {0pt}%     Space after thm head: " " = normal interword space;
  {%
   \makebox[0pt][r]{\makebox[\jessicaindent][l]{#2}}%
   #1%
   \@ifnotempty{#3}{\textnormal{ (#3)}}%    
   \hspace{\jessicalabelsep}%
  }
\makeatother

\newlength{\jessicaindent}
\newlength{\jessicalabelsep}
\setlength{\jessicaindent}{4em}
\setlength{\jessicalabelsep}{0.5em}

\theoremstyle{jessica}
\newtheorem{jessicacustom}{\jessicacustomname}[section]
\newtheorem*{jessicacustomnonumber}{\jessicacustomname}

\ExplSyntaxOn
\keys_define:nn { jessica/custom }
 {
  number   .bool_set:N         = \l_jessica_custom_numbered_bool,
  number   .default:n          = true,
  number   .initial:n          = true,
  nonumber .bool_set_inverse:N = \l_jessica_custom_numbered_bool,
  nonumber .default:n          = true,
  name     .tl_set:N           = \l_jessica_custom_name_tl,
  name     .initial:n          = Custom,
  attr .tl_set:N = \l_jessica_custom_attr_tl,
 }
\tl_new:N \l__jessica_custom_temp_tl

\NewDocumentEnvironment{custom}{O{}}
 {
  \keys_set:nn { jessica/custom } { #1 }
  \tl_set_eq:NN \jessicacustomname \l_jessica_custom_name_tl
  \tl_set:Nx \l__jessica_custom_temp_tl
   {
    \bool_if:NTF \l_jessica_custom_numbered_bool
     { \exp_not:N \jessicacustom }
     { \exp_not:N \jessicacustomnonumber }
    \tl_if_empty:NF \l_jessica_custom_attr_tl
     { [\exp_not:V \l_jessica_custom_attr_tl] }
   }
  \tl_use:N \l__jessica_custom_temp_tl
 }
 {
  \bool_if:NTF \l_jessica_custom_numbered_bool
   { \endjessicacustom }
   { \endjessicacustomnonumber }
 }
\ExplSyntaxOff

\begin{document}

\section{One}

\lipsum[4]

\begin{custom}[name=Custom Title,attr=optional words]\label{A}
\lipsum*[2]
\end{custom}

\begin{custom}\label{B}
Even though I've been teaching for n! years, every class is a fresh
adventure -- An adventure. That word is a euphemism for the stark reality,
which is that every class contains some totally shocking development
that I have never seen before and haven't any idea how to cope with.

Just last year, for the first time in many years, I taught a section
of the junior-level mathematical analysis course. You know --  the one
where students meet proofs in analysis for the first time. The one where
students and epsilons meet, eyeball to eyeball, and it isn't the epsilons
that blink. The one where students decide that they really wanted to be
doctors and lawyers after all.
\end{custom}

\begin{custom}[name=Notnumbered,nonumber,attr=xyz]
This is not numbered.
\end{custom}

Here are the references: \ref{A} and \ref{B}.

\end{document}

在此处输入图片描述


语句缩进的版本,代码改编自https://tex.stackexchange.com/a/236407/4427

\documentclass{article}
\usepackage{amsthm,xparse,etoolbox}

\usepackage{lipsum}

\makeatletter
\newlength{\jessicaindent}
\newlength{\jessicalabelsep}
\setlength{\jessicaindent}{4em}
\setlength{\jessicalabelsep}{0.5em}

\newtheoremstyle{jessica}% name
  {\topsep}%      Space above
  {\topsep}%      Space below
  {\normalfont}%         Body font
  {}%         Indent amount (empty = no indent, \parindent = para indent)
  {\bfseries}% Thm head font
  {}%        Punctuation after thm head
  {0pt}%     Space after thm head: " " = normal interword space;
  {%
   \makebox[0pt][r]{\makebox[\jessicaindent][l]{#2}}%
   #1%
   \@ifnotempty{#3}{\textnormal{ (#3)}}%    
   \hspace{\jessicalabelsep}%
  }

\theoremstyle{jessica}
\newtheorem{jessicacustom}{\jessicacustomname}[section]
\newtheorem*{jessicacustomnonumber}{\jessicacustomname}

\NewDocumentEnvironment{custom}{G{Custom}}
 {%
  \patchcmd{\@thm}{\trivlist}{\list{}{\leftmargin=\jessicaindent}}{}{}%
  \def\jessicacustomname{#1}\jessicacustom
 }
 {\endjessicacustom}
\patchcmd{\endjessicacustom}{\endtrivlist}{\endlist}{}{}

\NewDocumentEnvironment{custom*}{G{Custom}}
 {%
  \patchcmd{\@thm}{\trivlist}{\list{}{\leftmargin=\jessicaindent}}{}{}%
  \def\jessicacustomname{#1}\jessicacustomnonumber
 }
 {\endjessicacustomnonumber}
\patchcmd{\endjessicacustomnonumber}{\endtrivlist}{\endlist}{}{}
\makeatother

\begin{document}

\section{One}

\lipsum[4]

\begin{custom}{Custom Title}[optional words]\label{A}
\lipsum*[2]
\end{custom}

\begin{custom}\label{B}
Even though I've been teaching for n! years, every class is a fresh
adventure -- An adventure. That word is a euphemism for the stark reality,
which is that every class contains some totally shocking development
that I have never seen before and haven't any idea how to cope with.

Just last year, for the first time in many years, I taught a section
of the junior-level mathematical analysis course. You know --  the one
where students meet proofs in analysis for the first time. The one where
students and epsilons meet, eyeball to eyeball, and it isn't the epsilons
that blink. The one where students decide that they really wanted to be
doctors and lawyers after all.
\end{custom}

\begin{custom*}{Notnumbered}[xyz]
This is not numbered.
\end{custom*}

Here are the references: \ref{A} and \ref{B}.

\end{document}

在此处输入图片描述

和key-value接口一样。

\documentclass{article}
\usepackage{amsthm,xparse,etoolbox}

\usepackage{lipsum}

\makeatletter
\newlength{\jessicaindent}
\newlength{\jessicalabelsep}
\setlength{\jessicaindent}{4em}
\setlength{\jessicalabelsep}{0.5em}

\newtheoremstyle{jessica}% name
  {\topsep}%      Space above
  {\topsep}%      Space below
  {\normalfont}%         Body font
  {}%         Indent amount (empty = no indent, \parindent = para indent)
  {\bfseries}% Thm head font
  {}%        Punctuation after thm head
  {0pt}%     Space after thm head: " " = normal interword space;
  {%
   \makebox[0pt][r]{\makebox[\jessicaindent][l]{#2}}%
   #1%
   \@ifnotempty{#3}{\textnormal{ (#3)}}%    
   \hspace{\jessicalabelsep}%
  }

\theoremstyle{jessica}
\newtheorem{jessicacustom}{\jessicacustomname}[section]
\newtheorem*{jessicacustomnonumber}{\jessicacustomname}
\patchcmd{\endjessicacustom}{\endtrivlist}{\endlist}
\patchcmd{\endjessicacustomnonumber}{\endtrivlist}{\endlist}

\makeatother

\ExplSyntaxOn
\keys_define:nn { jessica/custom }
 {
  number   .bool_set:N         = \l_jessica_custom_numbered_bool,
  number   .default:n          = true,
  number   .initial:n          = true,
  nonumber .bool_set_inverse:N = \l_jessica_custom_numbered_bool,
  nonumber .default:n          = true,
  name     .tl_set:N           = \l_jessica_custom_name_tl,
  name     .initial:n          = Custom,
  attr .tl_set:N = \l_jessica_custom_attr_tl,
 }

\makeatletter
\NewDocumentEnvironment{custom}{O{}}
 {
  \patchcmd{\@thm}{\trivlist}{\list{}{\leftmargin=\jessicaindent}}{}{}
  \keys_set:nn { jessica/custom } { #1 }
  \tl_set_eq:NN \jessicacustomname \l_jessica_custom_name_tl
  \tl_set:Nx \l__jessica_custom_temp_tl
   {
    \bool_if:NTF \l_jessica_custom_numbered_bool
     { \exp_not:N \jessicacustom }
     { \exp_not:N \jessicacustomnonumber }
    \tl_if_empty:NF \l_jessica_custom_attr_tl
     { [\exp_not:V \l_jessica_custom_attr_tl] }
   }
  \tl_use:N \l__jessica_custom_temp_tl
 }
 {
  \bool_if:NTF \l_jessica_custom_numbered_bool
   { \endjessicacustom }
   { \endjessicacustomnonumber }
 }
\makeatother
\ExplSyntaxOff

\begin{document}

\section{One}

\lipsum[4]

\begin{custom}[name=Custom Title,attr=optional words]\label{A}
\lipsum*[2]
\end{custom}

\begin{custom}\label{B}
Even though I've been teaching for n! years, every class is a fresh
adventure -- An adventure. That word is a euphemism for the stark reality,
which is that every class contains some totally shocking development
that I have never seen before and haven't any idea how to cope with.

Just last year, for the first time in many years, I taught a section
of the junior-level mathematical analysis course. You know --  the one
where students meet proofs in analysis for the first time. The one where
students and epsilons meet, eyeball to eyeball, and it isn't the epsilons
that blink. The one where students decide that they really wanted to be
doctors and lawyers after all.
\end{custom}

\begin{custom}[name=Notnumbered,nonumber,attr=xyz]
This is not numbered.
\end{custom}

Here are the references: \ref{A} and \ref{B}.

\end{document}

答案2

您希望同时做到这两点

\begin{custom}

\begin{custom}{Custom Title}[optional words]

。尽管 -environment 的第一个参数custom短语{Custom Title}嵌套在花括号中,但实际上它也是一个可选参数。

一方面,我怀疑重载花括号语法(通常用于表示强制参数)并用它来表示可选参数不是一个好主意。

另一方面,您不能使用方括号连续提供可选参数:如果您没有提供所有参数,则不清楚您希望提供哪些参数以及您希望省略哪些参数以获得其默认值。

已经有人建议你使用一些 keyval 接口。
我认为这是解决问题的最佳方法。

是因为它可能:

为了好玩,我实施了一个粗略的黑客攻击来拦截\csname..\endcsname环境名称的评估:

您已定义了一个名为 的环境custom,以及另一个名为 的环境custom*

您不需要直接向 -macro 提供\begin其中一个<environment-names>参数,而是可以调用该宏\Customtitle

句法:

\Customtitle[<Custom title>]{<environment-name>}
 ; <environment-name>  = custom  or  custom*

综合起来:

First example:

  \begin{custom}
  ...
  \end{custom}

Second example:

  \begin{custom}[optional words]
  ...
  \end{custom}

Third example:

  \begin{\Customtitle[<Custom title>]{custom}}
  ...
  \end{custom}

Fourth example:

  \begin{\Customtitle[<Custom title>]{custom}}[optional words]
  ...
  \end{custom}

Fifth example - this is equivalent to the first example:

  \begin{\Customtitle{custom}}
  ...
  \end{custom}

Sixth example - this is equivalent to the second example:

  \begin{\Customtitle{custom}}[optional words]
  ...
  \end{custom}

-----------------------------------------------------------

Seventh example:

  \begin{custom*}
  ...
  \end{custom*}

Eighth example:

  \begin{custom*}[optional words]
  ...
  \end{custom*}

Nineth example:

  \begin{\Customtitle[<Custom title>]{custom*}}
  ...
  \end{custom*}

Tenth example:

  \begin{\Customtitle[<Custom title>]{custom*}}[optional words]
  ...
  \end{custom*}

Eleventh example - this is equivalent to the seventh example:

  \begin{\Customtitle{custom*}}
  ...
  \end{custom*}

Twelveth example - this is equivalent to the eighth example:

  \begin{\Customtitle{custom*}}[optional words]
  ...
  \end{custom*}

 

\documentclass{book}
\usepackage{amsthm}
\usepackage{enumitem}
\usepackage{xparse}
\usepackage{hyperref}

\usepackage{lipsum}

\newtheoremstyle{fctaylor}% name
  {\topsep}%      Space above
  {\topsep}%      Space below
  {\normalfont}%         Body font
  {}%         Indent amount (empty = no indent, \parindent = para indent)
  {\bfseries}% Thm head font
  {}%        Punctuation after thm head
  {0pt}%     Space after thm head: " " = normal interword space;
  {\makethmhead{#1}{#2}{#3}}

\newlength\fctaylortheoremindent
\AtBeginDocument{\setlength\fctaylortheoremindent{3em}} % <- customize here
\newlength\fctaylorlabelsep
\AtBeginDocument{\setlength\fctaylorlabelsep{1em}} % <- customize here

\makeatletter
\newcommand{\makethmhead}[3]{%
  \gdef\thisthmhead{%
    \makebox[\fctaylortheoremindent][l]{\bfseries#2}%
    {\bfseries#1}%
    \@ifnotempty{#3}{ (#3)}%
    \hspace{\fctaylorlabelsep}%
    \phantomsection%%% THIS LINE ADDED
  }%
}
\makeatother

\newenvironment{fctayloritemize}
 {\list{}{%
    \leftmargin=\fctaylortheoremindent
    \labelwidth=\dimexpr\fctaylortheoremindent-\labelsep\relax
    \itemindent=0pt
  }}
 {\endlist}

\NewDocumentCommand{\newfctaylortheorem}{smomo}{%
  \IfBooleanTF{#1}
   {\newtheorem*{fctaylor@#2}{#4}}
   {\IfNoValueTF{#3}
     {\IfNoValueTF{#5}
       {\newtheorem{fctaylor@#2}{#4}}
       {\newtheorem{fctaylor@#2}{#4}[#5]}}
     {\newtheorem{fctaylor@#2}[fctaylor@#3]{#4}}}%
  \NewDocumentEnvironment{#2}{o}
   {\IfNoValueTF{##1}{\begin{fctaylor@#2}}{\begin{fctaylor@#2}[##1]}%
    \begin{fctayloritemize}\item[\thisthmhead\hfill]}
   {\end{fctayloritemize}\end{fctaylor@#2}}%
}

\theoremstyle{fctaylor}
\newfctaylortheorem{mytheorem}{Theorem}[chapter]
\newcommand{\thistheoremname}{}
\newfctaylortheorem{genericthm}[mytheorem]{\thistheoremname}

\makeatletter
\newcommand\Customtitle{@Customtitle\endcsname}
\newcommand\@Customtitle[2][Custom]{%
  \@ifundefined{#2}{%
    \def\reserved@a{\@latex@error{Environment #2 undefined}\@eha}%
  }{%
    \def\reserved@a{%
      \def\@currenvir{#2}%
      \renewcommand{\thistheoremname}{#1}%
      \expandafter\expandafter\expandafter\@gobbletwo\csname#2\endcsname
    }%
  }%
  \csname reserved@a%
}%
\newenvironment{custom}%
  {\@firstofone{\renewcommand{\thistheoremname}{Custom}}%
   \begin{genericthm}}
  {\end{genericthm}}
\newfctaylortheorem*{genericthm*}{\thistheoremname}
\newenvironment{custom*}%
  {\@firstofone{\renewcommand{\thistheoremname}{Custom}}%
   \begin{genericthm*}}
  {\end{genericthm*}}
\makeatother

\begin{document}
\section{One}

\begin{custom}\label{A}
\lipsum*[2]
\end{custom}

\begin{\Customtitle{custom}}\label{B}
\lipsum*[2]
\end{custom}

\begin{custom}[optional words]\label{C}
\lipsum*[2]
\end{custom}

\begin{\Customtitle{custom}}[optional words]\label{D}
\lipsum*[2]
\end{custom}

\begin{\Customtitle[Custom Title]{custom}}\label{E}
\lipsum*[2]
\end{custom}

\begin{\Customtitle[Custom Title]{custom}}[optional words]\label{F}
\lipsum*[2]
\end{custom}

\newpage

\begin{custom*}\label{G}
\lipsum*[2]
\end{custom*}

\begin{\Customtitle{custom*}}\label{H}
\lipsum*[2]
\end{custom*}

\begin{custom*}[optional words]\label{I}
\lipsum*[2]
\end{custom*}

\begin{\Customtitle{custom*}}[optional words]\label{J}
\lipsum*[2]
\end{custom*}

\begin{\Customtitle[Custom Title]{custom*}}\label{K}
\lipsum*[2]
\end{custom*}

\begin{\Customtitle[Custom Title]{custom*}}[optional words]\label{L}
\lipsum*[2]
\end{custom*}

\newpage

Here are the references:\bigskip

Here the hyperlinks go to the hyperlink-anchors created by the 
theorems via \verb|\phantompage| while textual phrases denote 
the numbers of the theorems. As the counter of \verb|custom|-theorems
is bound do the chapter counter, the theorem-numbers are of pattern
\verb|<chapter>.<theorem>|. With \verb|<chapter>=0| you get the
pattern \verb|0.<theorem>|:\bigskip

\ref{A} and \ref{B} and \ref{C} and \ref{D} and \ref{E}
and \ref{F}.\bigskip

Here the hyperlinks go to the hyperlink-anchors created by the
theorems via \verb|\phantompage| while textual phrases denote
the number of the section in question as \verb|custom*|-theorems
don't have a counter. We are in chapter 0, section 1, thus the
section number is 0.1, thus you always get \verb|0.1|:\bigskip

\ref{G} and \ref{H} and \ref{I} and \ref{J} and \ref{K}
and \ref{L}.

\end{document}

相关内容