如何在“newenvironment”定义中将参数与给定字符串进行比较?

如何在“newenvironment”定义中将参数与给定字符串进行比较?

问题描述

我正在尝试创建一个可以产生具有不同风格的盒装段落的环境。

因此,我尝试实现一些“if”-“elif”-“else”块,将传递给环境的参数与一些给定的关键字进行比较,以决定将哪种样式应用于段落。

不幸的是,实现这个想法远没有听起来那么简单,因为我尝试过的所有方法(这里),以便将参数与字符串进行比较,但没有给出正确的结果。

平均能量损失

这是我尝试过的(我的灵感来自于这里):

\documentclass{article}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{ifthen}
%#################################
\newtcolorbox{mybox}[2][]{colback=black!5!white,colframe=black!45!white,fonttitle=\bfseries,enhanced,breakable,attach boxed title to top left={yshift*=-\tcboxedtitleheight/2},title={#2}}
%#################################
\makeatletter
\newcommand*\BEGININGcommands{}
\newcommand*\ENDINGcommands{}

\newenvironment{myENV}[1][]
{%
    \ifthenelse{\equal{#1}{keyword}}
    {
      % if '#1' == 'keyword' then apply the 'mybox' style to the selected paragraph
      \textcolor{green!35!white}{
      \textbf{Seeing this means that:} \fbox{\#1} and \fbox{keyword} represent the same string, which means that the content will be formatted by surrounding it with \textbf{mybox}.}\par\vspace{1em}
      \def\BEGININGcommands{\begin{mybox}[#1]}
      \def\ENDINGcommands{\end{mybox}}
    }
    {
      % else dont apply anything
      \textcolor{red!35!white}{\textbf{Seeing this means that:} \fbox{\#1} is not the same as \fbox{keyword}, which means that no formating will be applied.}\par\vspace{1em}
      \def\BEGININGcommands{}
      \def\ENDINGcommands{}
    }
    \BEGININGcommands
}
{%
    \ENDINGcommands%
}
\makeatother
%#################################
\begin{document}
\begin{myENV}{keywordA}
  This paragraph \textbf{should be formatted} as the \textbf{keywordA} is \textbf{specified}.
\end{myENV}
\par\vspace{2em}\noindent\rule{\textwidth}{1pt}\vspace{2em}
\begin{myENV}{keywordB}
  This paragraph \textbf{should not be formatted} as the \textbf{keywordA} is \textbf{not specified}.
\end{myENV}
\end{document}

输出

在此处输入图片描述

期望输出

在此处输入图片描述

答案1

您使用了错误的参数类型和数量。

如果选择不止一个,\ifthenelse就会变得非常麻烦。我提出了一个expl3解决方案,可以轻松容纳任意数量的关键字选择。


\documentclass{article}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}

\newtcolorbox{myboxA}[1]{
  colback=black!5!white,
  colframe=black!45!white,
  fonttitle=\bfseries,
  enhanced,
  breakable,
  attach boxed title to top left={yshift*=-\tcboxedtitleheight/2},
  title={#1}
}
\newtcolorbox{myboxB}[1]{
  colback=red!5!white,
  colframe=red!45!white,
  fonttitle=\bfseries,
  enhanced,
  breakable,
  attach boxed title to top left={yshift*=-\tcboxedtitleheight/2},
  title={#1}
}

\ExplSyntaxOn

\NewDocumentEnvironment{myENV}{O{}}
 {
  \str_case:nn { #1 }
   {
    {keywordA}{\begin{myboxA}{#1}}
    {keywordB}{\begin{myboxB}{#1}}
   }
 }
 {
  \str_case:nn { #1 }
   {
    {keywordA}{\end{myboxA}}
    {keywordB}{\end{myboxB}}
   }
 }

\ExplSyntaxOff

\begin{document}

\begin{myENV}[keywordA]
  This paragraph \textbf{should be formatted} as the \textbf{keywordA} is \textbf{specified}.
\end{myENV}

\begin{myENV}[keywordB]
  This paragraph \textbf{should be formatted} as the \textbf{keywordB} is \textbf{specified}.
\end{myENV}

\begin{myENV}[foo]
  No formatting here.
\end{myENV}

\end{document}

在此处输入图片描述

答案2

而不是创建单独的tcolorbox为每种可能性创建一个环境风格。每种样式都包含与格式相关的键值选项,然后您可以通过环境选项传递这些选项。

在此处输入图片描述

\documentclass{article}

\usepackage{xcolor}
\usepackage[most]{tcolorbox}

\tcbset{
  % Define base style common to all boxes
  base/.style = {
    fonttitle = \bfseries,
    enhanced,
    breakable,
    attach boxed title to top left={yshift*=-\tcboxedtitleheight/2}
  },
  % Define keywordA-specific box style additions/overrides
  keywordA/.style = {
    colback = black!5!white,
    colframe = black!45!white,
  },
  % Define keywordB-specific box style additions/overrides
  keywordB/.style = {
    colback = red!5!white,
    colframe = red!45!white
  }
}

\NewDocumentEnvironment{myENV}{ O{} m }{%
  \begin{tcolorbox}[
    base, % load base style
    #1, % load additional style based on optional argument
    title = {#2} % Title in second (mandatory) argument
  ]
}{%
  \end{tcolorbox}
}

\begin{document}

\begin{myENV}[keywordA]{Something A}
  This paragraph \textbf{should be formatted} as the \textbf{keywordA} is \textbf{specified}.
\end{myENV}

\begin{myENV}[keywordB]{Something B}
  This paragraph \textbf{should be formatted} as the \textbf{keywordB} is \textbf{specified}.
\end{myENV}

\end{document}

相关内容