与环境相关的文本格式(定理)

与环境相关的文本格式(定理)

我从另一篇文章中获得了下面的代码,我不确定这是否可行,但我想定义一个替代\myemph命令,其效果取决于它所嵌入的定理环境。

\documentclass[a4paper]{article}    
\usepackage{amsthm,eso-pic,thmtools,framed}
\usepackage{lipsum}

    \definecolor{localblue}{RGB}{55, 115, 180}
    \definecolor{localgreen}{RGB}{80, 140, 30}

\newcommand{\myemph}[1]{\emph{\textcolor{localblue}{#1}}}
\newcommand{\miemph}[1]{\emph{\textcolor{localgreen}{#1}}}


    \colorlet{headcolordefn}{localblue}
    \colorlet{rulecolordefn}{localblue}
    \newenvironment{leftbardefn}{%
        \def\FrameCommand{{\color{rulecolordefn}\vrule width 3pt} \hspace{10pt}}%
        \MakeFramed {\advance\hsize-\width \FrameRestore}}%
        {\endMakeFramed}
        \declaretheoremstyle[
          headfont=\sffamily\bfseries,%
          notefont=\sffamily\bfseries,%
          notebraces={}{},%
          headpunct=,%
          bodyfont=,%
          headformat=\color{headcolordefn}\NAME~\NUMBER\hfill\NOTE\smallskip\linebreak,%
          preheadhook=\begin{leftbardefn},
          postfoothook=\end{leftbardefn},%
                           ]{customDefinition}
\declaretheorem[
               title=Definition,style=customDefinition, numberwithin=section
               ]{defn}



    \colorlet{headcolorthm}{localgreen}
    \colorlet{rulecolorthm}{localgreen}
    \newenvironment{leftbarthm}{%
        \def\FrameCommand{{\color{rulecolorthm}\vrule width 3pt} \hspace{10pt}}%
        \MakeFramed {\advance\hsize-\width \FrameRestore}}%
        {\endMakeFramed}
        \declaretheoremstyle[
        headfont=\sffamily\bfseries,%
        notefont=\sffamily\bfseries,%
        notebraces={}{},%
        headpunct=,%
        bodyfont=,%
        headformat=\color{headcolorthm}\NAME~\NUMBER\hfill\NOTE\smallskip\linebreak,%
        preheadhook=\begin{leftbarthm},
        postfoothook=\end{leftbarthm},%
                         ]{customTheorem}
\declaretheorem[
               title=Theorem,style=customTheorem, numberwithin=section, sibling=defn
               ]{thrm}

 
\begin{document}
\lipsum[2]

            \begin{defn}[object]
                Such-and-such is said to be an \myemph{object} when so-and-so is verified.
            \end{defn}
\lipsum[2]
            \begin{thrm}[of the day]
                If such-and-such \miemph{conditions} are met, then we're happy.
            \end{thrm}
\lipsum[2]

\end{document}

我有一系列具有特定颜色的不同“定理”样式,并且我希望我的命令根据样式中定义的文本为文本着色,例如,我希望它呈现

在此处输入图片描述

使用前面的代码。有没有办法可以轻松完成此操作?

编辑:根据评论的要求,我修改了示例以提供更详细的示例。它呈现:

在此处输入图片描述

所以我的问题实际上是:有没有办法将 \myemph 和 \miemph 命令合并为一个(当然还有比这两个更多的环境:命题、推论......)?

答案1

下面的例子演示了我所建议的此评论

  • 所有类定理环境都共享一个定理风格,称为thm@custom
  • 这个定理样式利用了环境thm@leftbar
  • thm@custom样式和环境中,使用thm@leftbar主题颜色,而不是和等特定颜色。thm@themelocalbluelocalgreen
  • 在每一种类定理环境的开始处,颜色thm@theme被设置为相应的特定颜色。这是通过 来实现的\AtBeginEnvironment

此外,\DeclareEmphSequence用于配置的样式\emph,因此您可以直接\emph在类似 theorem 的环境中使用并获取所需主题颜色的文本。

\documentclass[a4paper]{article}    
\usepackage{amsthm,eso-pic,thmtools,framed}
\usepackage{lipsum}

    \definecolor{localblue}{RGB}{55, 115, 180}
    \definecolor{localgreen}{RGB}{80, 140, 30}

    \newenvironment{thm@leftbar}{%
        \def\FrameCommand{{\color{thm@theme}\vrule width 3pt} \hspace{10pt}}%
        \MakeFramed {\advance\hsize-\width \FrameRestore}}%
        {\endMakeFramed}
    \declaretheoremstyle[
          headfont=\sffamily\bfseries,%
          notefont=\sffamily\bfseries,%
          notebraces={}{},%
          headpunct=,%
          bodyfont=,%
          headformat=\color{thm@theme}\NAME~\NUMBER\hfill\NOTE\smallskip\linebreak,%
          preheadhook=\begin{thm@leftbar},
          postfoothook=\end{thm@leftbar},%
                           ]{thm@custom}
\declaretheorem[
               title=Definition,style=thm@custom, numberwithin=section
               ]{defn}
\declaretheorem[
               title=Theorem,style=thm@custom, numberwithin=section, sibling=defn
               ]{thrm}

% since latex2e 2020-10-01
% for older latex2e, load etoolbox package
\AtBeginEnvironment{defn}{\colorlet{thm@theme}{localblue}\thmemph}
\AtBeginEnvironment{thrm}{\colorlet{thm@theme}{localgreen}\thmemph}

% since latex2e 2020-02-02
\newcommand\thmemph{%
  \DeclareEmphSequence{\itshape\color{thm@theme}}}

\begin{document}
\lipsum[2]

            \begin{defn}[object]
                Such-and-such is said to be an \emph{object} when so-and-so is verified.
            \end{defn}
\lipsum[2]
            \begin{thrm}[of the day]
                If such-and-such \emph{conditions} are met, then we're happy.
            \end{thrm}
\lipsum[2]

test \verb|\emph| \emph{outside thm-like envs}
\end{document}

在此处输入图片描述

相关内容