在以下 MWE 中,当提到参数但标题为空时,我想删除与标题相关的空格##1
。这意味着在声明中更改IfValueTF
为。IfNoValueOrEmptyTF
tcolorbox
是否有可能做到这一点?
\documentclass{scrbook}
\RequirePackage{scrhack}
\RequirePackage{luacode,luatextra}
\RequirePackage[fleqn]{mathtools,stmaryrd}
\RequirePackage[warnings-off={mathtools-colon,mathtools-overbracket}]{unicode-math}
\RequirePackage{pdflscape}
\PassOptionsToPackage{hyphens}{url}
\RequirePackage{hyperref}
%
\RequirePackage[capitalize,nameinlink]{cleveref}
\newcommand{\crefrangeconjunction}{ et~}
\newcommand{\crefpairconjunction}{ et~}
\newcommand{\crefmiddleconjunction}{, }
\RequirePackage{totcount}
%
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\IfNoValueOrEmptyTF}{mmm}
{
\IfNoValueTF{#1}
{#2} % true
{\tl_if_empty:nTF {#1} {#2} {#3}} % false
}
\ExplSyntaxOff
%
\RequirePackage{xargs}
\RequirePackage{centernot,cancel}
\RequirePackage{ifthen}
\RequirePackage{etoolbox,refcount}
\RequirePackage{xpatch}
\RequirePackage{color}
\RequirePackage[usenames,dvipsnames,svgnames,table,x11names]{xcolor}
\definecolor{monbleu}{HTML}{003366}
%
%Encadrement
\RequirePackage[skins,breakable,xparse,many]{tcolorbox}
%--------------
%Définition/Théorème/etc.
%
\BeforeBeginEnvironment{tcolorbox}{\savenotes}
\AfterEndEnvironment{tcolorbox}{\spewnotes}
%
\newcommand*\ifcounter[1]{%
\ifcsname c@#1\endcsname
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
%
\NewDocumentCommand\DefinirTheoreme{m m m m m m}{%
\DeclareTColorBox[auto counter,
number within = chapter,
crefname = {#5}{#6}]{#1}{ o o }{
enhanced ,
coltitle = #3 ,
colframe = #4 ,
colbacktitle = white ,
boxrule = 0.5pt ,
toprule at break = 0mm ,
bottomrule at break = 0mm ,
breakable ,
attach boxed title to top left={xshift=3mm, yshift=-3mm, yshifttext=-1mm},
IfValueTF={##1}{title=#2 \thetcbcounter {~|~} {\color{black}\normalfont\bfseries ##1}}{title=#2 \thetcbcounter},
IfValueTF={##2}{##2}{},
}
}%
\DefinirTheoreme{defi}{Définition}{monbleu}{monbleu}{Définition}{Définitions}
%%%%
\begin{document}
\begin{defi}[][label={croiss}]
Soit \((x,y)\in \left(\mathbb{R}_+\right)^2\) tel que \(x<y\) (resp. \(x\leqslant y)\), alors
\[ x^2 < y^2 \;\; (\text{resp. }x^2 \leqslant y^2 ), \sqrt{x} < \sqrt{y}.\]
\end{defi}
\begin{defi}
Soit \((x,y)\in \left(\mathbb{R}_+\right)^2\) tel que \(x<y\) (resp. \(x\leqslant y)\), alors
\[ x^2 < y^2 \;\; (\text{resp. }x^2 \leqslant y^2 ), \sqrt{x} < \sqrt{y}.\]
\end{defi}
\end{document}
答案1
我做的改变:
\DeclareTColorBox
将from的第三个参数更改{ o o }
为{ O{} O{} }
。- 在第四个论点中
\DeclareTColorBox
,改变
到IfValueTF={##1}{title=#2 \thetcbcounter {~|~} {\color{black}\normalfont\bfseries ##1}}{title=#2 \thetcbcounter}, IfValueTF={##2}{##2}{},
IfEmptyTF={##1}% {title=#2 \thetcbcounter}% {title=#2 \thetcbcounter {~|~}{\color{black}\normalfont\bfseries ##1}}, ##2
完整示例:
% !TeX TS-program = lualatex
\documentclass{scrbook}
\RequirePackage{scrhack}
\RequirePackage{luacode,luatextra}
\RequirePackage[fleqn]{mathtools,stmaryrd}
\RequirePackage[warnings-off={mathtools-colon,mathtools-overbracket}]{unicode-math}
\RequirePackage{pdflscape}
\PassOptionsToPackage{hyphens}{url}
\RequirePackage{hyperref}
%
\RequirePackage[capitalize,nameinlink]{cleveref}
\newcommand{\crefrangeconjunction}{ et~}
\newcommand{\crefpairconjunction}{ et~}
\newcommand{\crefmiddleconjunction}{, }
\RequirePackage{totcount}
%
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\IfNoValueOrEmptyTF}{mmm}
{
\IfNoValueTF{#1}
{#2} % true
{\tl_if_empty:nTF {#1} {#2} {#3}} % false
}
\ExplSyntaxOff
%
\RequirePackage{xargs}
\RequirePackage{centernot,cancel}
\RequirePackage{ifthen}
\RequirePackage{etoolbox,refcount}
\RequirePackage{xpatch}
\RequirePackage{color}
\RequirePackage[usenames,dvipsnames,svgnames,table,x11names]{xcolor}
\definecolor{monbleu}{HTML}{003366}
%
%Encadrement
\RequirePackage[skins,breakable,xparse,many]{tcolorbox}
%--------------
%Définition/Théorème/etc.
%
\BeforeBeginEnvironment{tcolorbox}{\savenotes}
\AfterEndEnvironment{tcolorbox}{\spewnotes}
%
\newcommand*\ifcounter[1]{%
\ifcsname c@#1\endcsname
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
%
\NewDocumentCommand\DefinirTheoreme{m m m m m m}{%
\DeclareTColorBox[auto counter,
number within = chapter,
crefname = {#5}{#6}]{#1}{ O{} O{} }
{
enhanced ,
coltitle = #3 ,
colframe = #4 ,
colbacktitle = white ,
boxrule = 0.5pt ,
toprule at break = 0mm ,
bottomrule at break = 0mm ,
breakable ,
attach boxed title to top left={xshift=3mm, yshift=-3mm, yshifttext=-1mm},
IfEmptyTF={##1}%
{title=#2 \thetcbcounter}%
{title=#2 \thetcbcounter {~|~}{\color{black}\normalfont\bfseries ##1}},
##2,
}%
}
\DefinirTheoreme{defi}{Définition}{monbleu}{monbleu}{Définition}{Définitions}
%%%%
\begin{document}
\begin{defi}[][label={croiss}]
Soit \((x,y)\in \left(\mathbb{R}_+\right)^2\) tel que \(x<y\) (resp. \(x\leqslant y)\), alors
\[ x^2 < y^2 \;\; (\text{resp. }x^2 \leqslant y^2 ), \sqrt{x} < \sqrt{y}.\]
\end{defi}
\begin{defi}
Soit \((x,y)\in \left(\mathbb{R}_+\right)^2\) tel que \(x<y\) (resp. \(x\leqslant y)\), alors
\[ x^2 < y^2 \;\; (\text{resp. }x^2 \leqslant y^2 ), \sqrt{x} < \sqrt{y}.\]
\end{defi}
\begin{defi}[non-empty]
Soit \((x,y)\in \left(\mathbb{R}_+\right)^2\) tel que \(x<y\) (resp. \(x\leqslant y)\), alors
\[ x^2 < y^2 \;\; (\text{resp. }x^2 \leqslant y^2 ), \sqrt{x} < \sqrt{y}.\]
\end{defi}
\end{document}