根据我当前所处的环境更改标签颜色

根据我当前所处的环境更改标签颜色

这是我当前的 preamble.tex

\documentclass{report}

% basics
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{sidenotes}
\usepackage{textcomp}
\usepackage{url}
\usepackage{hyperref}
\hypersetup{
  colorlinks,
  linkcolor={black},
  citecolor={black},
  urlcolor={blue!80!black}
}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{float}
\usepackage{booktabs}
\usepackage[shortlabels]{enumitem}
\usepackage{emptypage}
\usepackage{subcaption}
\usepackage{multicol}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{amsmath, amsfonts, mathtools, amsthm, amssymb}
\usepackage{mathrsfs}
\usepackage{cancel}
\usepackage{bm}
\usepackage{systeme}
\usepackage{stmaryrd}

% theorems
\usepackage{thmtools}
\usepackage[framemethod=TikZ]{mdframed}
\mdfsetup{skipabove=1em,skipbelow=0em, innertopmargin=5pt, innerbottommargin=6pt}

\theoremstyle{definition}

\makeatletter

\@ifclasswith{report}{nocolor}{
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont, mdframed={ nobreak } ]{thmgreenbox}
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont, mdframed={ nobreak } ]{thmredbox}
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont]{thmbluebox}
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont]{thmblueline}
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont, numbered=no, mdframed={ rightline=false, topline=false, bottomline=false, }, qed=\qedsymbol ]{thmproofbox}
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont, numbered=no, mdframed={ nobreak, rightline=false, topline=false, bottomline=false } ]{thmexplanationbox}
  \AtEndEnvironment{eg}{\null\hfill$\diamond$}%
}{
  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{ForestGreen!70!black}, bodyfont=\normalfont,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=ForestGreen, backgroundcolor=ForestGreen!5,
  }
  ]{thmgreenbox}

  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{NavyBlue!70!black}, bodyfont=\normalfont,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=NavyBlue, backgroundcolor=NavyBlue!5,
  }
  ]{thmbluebox}

  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{NavyBlue!70!black}, bodyfont=\normalfont,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=NavyBlue
  }
  ]{thmblueline}

  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{RawSienna!70!black}, bodyfont=\normalfont,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=RawSienna, backgroundcolor=RawSienna!5,
  }
  ]{thmredbox}

  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{RawSienna!70!black}, bodyfont=\normalfont,
  numbered=no,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=RawSienna, backgroundcolor=RawSienna!1,
  },
  qed=\color{RawSienna!70!black}\qedsymbol
  ]{thmproofbox}

  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{NavyBlue!70!black}, bodyfont=\normalfont,
  numbered=no,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=NavyBlue, backgroundcolor=NavyBlue!1,
  },
  ]{thmexplanationbox}
}

\@ifclasswith{report}{nocolor}{
  \declaretheorem[style=thmproofbox, name=Proof]{replacementproof}
  \declaretheorem[style=thmexplanationbox, name=Proof]{explanation}
  \renewenvironment{proof}[1][\proofname]{\begin{replacementproof}}{\end{replacementproof}}
}{
  \declaretheorem[style=thmproofbox, name=Proof]{replacementproof}
  \renewenvironment{proof}[1][\proofname]{\vspace{-10pt}\begin{replacementproof}}{\end{replacementproof}}

  \declaretheorem[style=thmexplanationbox, name=Proof]{tmpexplanation}
  \newenvironment{explanation}[1][]{\vspace{-10pt}\begin{tmpexplanation}}{\end{tmpexplanation}}
}

\makeatother

\declaretheorem[style=thmgreenbox, name=Definition]{definition}
\declaretheorem[style=thmredbox, name=Proposition]{prop}
\declaretheorem[style=thmredbox, name=Theorem]{theorem}
\declaretheorem[style=thmredbox, name=Lemma]{lemma}
\declaretheorem[style=thmredbox, numbered=no, name=Corollary]{corollary}
\declaretheorem[style=thmblueline, numbered=no, name=Remark]{remark}
\declaretheorem[style=thmblueline, numbered=no, name=Example]{example}
\declaretheorem[style=thmblueline, numbered=no, name=Solution]{solution}

\newtheorem{exc}{Exercise}
\newtheorem*{notation}{Notation}
\newtheorem*{note}{Note}
\newtheorem*{previouslyseen}{As previously seen}
\newtheorem*{problem}{Problem}
\newtheorem*{observe}{Observe}
\newtheorem*{property}{Property}
\newtheorem*{intuition}{Intuition}

\begin{document}
  \begin{definition}
    \begin{table}[H]
      \label{tbl:degrees_into_radians}
      \begin{tabular}{|c|c|c|c|}
        \hline
        $1$, $2$, $3$, $4$
        \hline
      \end{tabular}
      \caption{Some Cool Label}
    \end{table}
  \end{definition}
\end{document}

如何让标签颜色从环境中更改为边框颜色?在此示例中,如何将标签颜色更改为绿色(如定义环境)?

答案1

下列的这个答案,下面基于 MWE 提出了一种可能的解决方案标题

\documentclass{article}
\usepackage{caption}
\usepackage{xcolor}

\captionsetup[table]{labelfont={color=green!50!black},font={color=green}}
\captionsetup[figure]{labelfont={color=red},font={color=red!50}}

\begin{document}

\begin{table}[htp]
  \caption{A table}
\end{table}

\begin{figure}[htp]
  \caption{A figure}
\end{figure}

\end{document}

在此处输入图片描述

个人想法:我总是觉得green它太亮了,通常很难看,我更喜欢 green!50!black


编辑-改进的答案

让我们引用标题的手册第 4 页。

\captionsetup 只对当前环境有影响,这一点很重要。因此,如果您只想更改当前图形或表格的设置,只需将 \captionsetup 命令放在图形或表格内 \caption 命令之前即可。

\captionsetup因此,我们的想法是,在每个里面定义一个特定的\newenvironment。在 MWE 中,这些\newenvironmentmySpecialEnvironment

我也用过这个答案让所有标题保持在同一页面上。

\documentclass{article}
\usepackage{xcolor}
\usepackage{caption}
\captionsetup[table]{labelfont={color=green!50!black},font={color=green}}
\captionsetup[figure]{labelfont={color=red},font={color=red!50}}

\newenvironment{mySpecialEnvironment}
  {\captionsetup[table]{labelfont={color=purple!75!black},font={color=purple}}
  \captionsetup[figure]{labelfont={color=orange!75!black},font={color=orange}}
  }
  {}

\setcounter{topnumber}{8}
\setcounter{bottomnumber}{8}
\setcounter{totalnumber}{8}
\begin{document}
\begin{table}[!ht]
  \caption{A table}
\end{table}
\begin{figure}[!ht]
  \caption{A figure}
\end{figure}
\begin{mySpecialEnvironment}
  \begin{table}[!ht]
    \caption{A table}
  \end{table}
  \begin{figure}[!ht]
    \caption{A figure}
  \end{figure}
\end{mySpecialEnvironment}
\begin{figure}[!ht]
\caption{A figure}
\end{figure}
\begin{table}[!ht]
\caption{A table}
\end{table}
\begin{figure}[!ht]
  \caption{A figure}
\end{figure}
\begin{mySpecialEnvironment}
  \begin{table}[!ht]
    \caption{A table}
  \end{table}
  \begin{figure}[!ht]
    \caption{A figure}
  \end{figure}
\end{mySpecialEnvironment}
\begin{figure}[!ht]
\caption{A figure}
\end{figure}
\end{document}

在此处输入图片描述

笔记:请注意,可能还有其他答案使用\makeatletter\makeatother类似于您的\declaretheorem,但在这里我没有寻找这些选项。

答案2

这是我的最终答案。非常感谢@FHZ

这个答案使用

\AtBeginEnvironment{<envname>}{
  \captionsetup{...}
}

定义每个环境中所有字幕的呈现方式。

\documentclass{report}

% basics
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{sidenotes}
\usepackage{textcomp}
\usepackage{url}
\usepackage{hyperref}
\hypersetup{
  colorlinks,
  linkcolor={black},
  citecolor={black},
  urlcolor={blue!80!black}
}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{float}
\usepackage{booktabs}
\usepackage[shortlabels]{enumitem}
\usepackage{emptypage}
\usepackage{subcaption}
\usepackage{multicol}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{amsmath, amsfonts, mathtools, amsthm, amssymb}
\usepackage{mathrsfs}
\usepackage{cancel}
\usepackage{bm}
\usepackage{systeme}
\usepackage{stmaryrd}

\usepackage{thmtools}
\usepackage[framemethod=TikZ]{mdframed}
\mdfsetup{skipabove=1em,skipbelow=0em, innertopmargin=5pt, innerbottommargin=6pt}

\theoremstyle{definition}

\colorlet{thmgreencolor}{ForestGreen!70!black}
\colorlet{thmredcolor}{RawSienna!70!black}
\colorlet{thmbluecolor}{NavyBlue!70!black}
\colorlet{thmblueline}{NavyBlue!70!black}
\colorlet{thmproofbox}{RawSienna!70!black}
\colorlet{thmexplanationbox}{NavyBlue!70!black}

\makeatletter

\@ifclasswith{report}{nocolor}{
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont, mdframed={ nobreak } ]{thmgreenbox}
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont, mdframed={ nobreak } ]{thmredbox}
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont]{thmbluebox}
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont]{thmblueline}
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont, numbered=no, mdframed={ rightline=false, topline=false, bottomline=false, }, qed=\qedsymbol ]{thmproofbox}
  \declaretheoremstyle[headfont=\bfseries\sffamily, bodyfont=\normalfont, numbered=no, mdframed={ nobreak, rightline=false, topline=false, bottomline=false } ]{thmexplanationbox}
  \AtEndEnvironment{eg}{\null\hfill$\diamond$}%
}{
  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{thmgreencolor}, bodyfont=\normalfont,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=ForestGreen, backgroundcolor=ForestGreen!5,
  }
  ]{thmgreenbox}

  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{thmredcolor}, bodyfont=\normalfont,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=RawSienna, backgroundcolor=RawSienna!5,
  }
  ]{thmredbox}

  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{thmbluecolor}, bodyfont=\normalfont,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=NavyBlue, backgroundcolor=NavyBlue!5,
  }
  ]{thmbluebox}

  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{thmblueline}, bodyfont=\normalfont,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=NavyBlue
  }
  ]{thmblueline}

  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{thmproofbox}, bodyfont=\normalfont,
  numbered=no,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=RawSienna, backgroundcolor=RawSienna!1,
  },
  qed=\color{RawSienna!70!black}\qedsymbol
  ]{thmproofbox}

  \declaretheoremstyle[
  headfont=\bfseries\sffamily\color{thmexplanationbox}, bodyfont=\normalfont,
  numbered=no,
  mdframed={
    linewidth=2pt,
    rightline=false, topline=false, bottomline=false,
    linecolor=NavyBlue, backgroundcolor=NavyBlue!1,
  },
  ]
  {thmexplanationbox}
}

\@ifclasswith{report}{nocolor}{
  \declaretheorem[style=thmproofbox, name=Proof]{replacementproof}
  \declaretheorem[style=thmexplanationbox, name=Proof]{explanation}
  \renewenvironment{proof}[1][\proofname]{\begin{replacementproof}}{\end{replacementproof}}
}{
  \declaretheorem[style=thmproofbox, name=Proof]{replacementproof}
  \renewenvironment{proof}[1][\proofname]{\vspace{-10pt}\begin{replacementproof}}{\end{replacementproof}}

  \declaretheorem[style=thmexplanationbox, name=Proof]{tmpexplanation}
  \newenvironment{explanation}[1][]{\vspace{-10pt}\begin{tmpexplanation}}{\end{tmpexplanation}}
}

\makeatother

\declaretheorem[style=thmgreenbox, name=Definition]{definition}
\declaretheorem[style=thmredbox, name=Proposition]{prop}
\declaretheorem[style=thmredbox, name=Theorem]{theorem}
\declaretheorem[style=thmredbox, name=Lemma]{lemma}
\declaretheorem[style=thmredbox, numbered=no, name=Corollary]{corollary}
\declaretheorem[style=thmblueline, numbered=no, name=Remark]{remark}
\declaretheorem[style=thmblueline, numbered=no, name=Example]{example}
\declaretheorem[style=thmblueline, numbered=no, name=Solution]{solution}

\AtBeginEnvironment{definition}{
  \captionsetup{labelfont={color=thmgreencolor}}
}
\AtBeginEnvironment{prop}{
  \captionsetup{labelfont={color=thmredcolor}}
}
\AtBeginEnvironment{theorem}{
  \captionsetup{labelfont={color=thmredcolor}}
}
\AtBeginEnvironment{lemma}{
  \captionsetup{labelfont={color=thmredcolor}}
}
\AtBeginEnvironment{corollary}{
  \captionsetup{labelfont={color=thmredcolor}}
}
\AtBeginEnvironment{remark}{
  \captionsetup{labelfont={color=thmbluecolor}}
}
\AtBeginEnvironment{example}{
  \captionsetup{labelfont={color=thmbluecolor}}
}
\AtBeginEnvironment{solution}{
  \captionsetup{labelfont={color=thmbluecolor}}
}

\newtheorem{exc}{Exercise}
\newtheorem*{notation}{Notation}
\newtheorem*{note}{Note}
\newtheorem*{previouslyseen}{As previously seen}
\newtheorem*{problem}{Problem}
\newtheorem*{observe}{Observe}
\newtheorem*{property}{Property}
\newtheorem*{intuition}{Intuition}

\begin{document}
  \begin{definition}[Some Definition]
    \lipsum[1]

    \begin{table}[H]
      \label{tbl:degrees_into_radians}
      \begin{tabular}{|c|c|c|c|}
        \hline
        $1$, $2$, $3$, $4$ \\
        \hline
      \end{tabular}
      \caption{Some Cool Label}
    \end{table}
  \end{definition}

  \newpage

  \begin{prop}
    \lipsum[1]

    \begin{table}[H]
      \label{tbl:degrees_into_radians}
      \begin{tabular}{|c|c|c|c|}
        \hline
        $1$, $2$, $3$, $4$ \\
        \hline
      \end{tabular}
      \caption{Some Cool Label}
    \end{table}
  \end{prop}

  \begin{theorem}[Some Theorem]
    \lipsum[1]

    \begin{table}[H]
      \label{tbl:degrees_into_radians}
      \begin{tabular}{|c|c|c|c|}
        \hline
        $1$, $2$, $3$, $4$ \\
        \hline
      \end{tabular}
      \caption{Some Cool Label}
    \end{table}
  \end{theorem}

  \begin{lemma}
    \lipsum[1]

    \begin{table}[H]
      \label{tbl:degrees_into_radians}
      \begin{tabular}{|c|c|c|c|}
        \hline
        $1$, $2$, $3$, $4$ \\
        \hline
      \end{tabular}
      \caption{Some Cool Label}
    \end{table}
  \end{lemma}

  \begin{corollary}
    \lipsum[1]

    \begin{table}[H]
      \label{tbl:degrees_into_radians}
      \begin{tabular}{|c|c|c|c|}
        \hline
        $1$, $2$, $3$, $4$ \\
        \hline
      \end{tabular}
      \caption{Some Cool Label}
    \end{table}
  \end{corollary}

  \newpage

  \begin{remark}
    \lipsum[1]

    \begin{table}[H]
      \label{tbl:degrees_into_radians}
      \begin{tabular}{|c|c|c|c|}
        \hline
        $1$, $2$, $3$, $4$ \\
        \hline
      \end{tabular}
      \caption{Some Cool Label}
    \end{table}
  \end{remark}

  \begin{example}
    \lipsum[1]

    \begin{table}[H]
      \label{tbl:degrees_into_radians}
      \begin{tabular}{|c|c|c|c|}
        \hline
        $1$, $2$, $3$, $4$ \\
        \hline
      \end{tabular}
      \caption{Some Cool Label}
    \end{table}
  \end{example}

  \begin{solution}
    \lipsum[1]

    \begin{table}[H]
      \label{tbl:degrees_into_radians}
      \begin{tabular}{|c|c|c|c|}
        \hline
        $1$, $2$, $3$, $4$ \\
        \hline
      \end{tabular}
      \caption{Some Cool Label}
    \end{table}
  \end{solution}
\end{document}

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

相关内容