是否可以对变量列表/数组进行字符串比较?
这是我的示例代码,勾选定义\currentbox
:
\documentclass{standalone}
\usepackage[most]{tcolorbox}
\makeatletter
% DEFINE BOX WITH GIVEN NAME
\newcommand\mybox[1]{%
\ifnum\pdf@strcmp{\unexpanded{#1}}{\currentbox}=0 %
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\setlength\fboxsep{0pt}\colorbox{teal}{\framebox(18,10){\textbf{\color{white}#1}}}}
{\framebox(18,10){\textbf{#1}}}%
}
\makeatother
% define current box
\def\currentbox{B}
\begin{document}
\begin{tcbitemize}[size=fbox,
colframe=white,
colback=white,
raster equal height,
raster force size=false,
raster equal skip=0pt,
raster columns=4]
\tcbitem[width=0.20\linewidth]
\mybox{A}
\tcbitem[width=0.20\linewidth]
\mybox{B}
\tcbitem[width=0.20\linewidth]
\mybox{C}
\tcbitem[width=0.20\linewidth]
\mybox{D}
\end{tcbitemize}%
\end{document}
因此,如果\currentbox
是一个包含“A”和“B”的列表/数组,并且如果strcmp
它在列表/数组中,则会生成:
答案1
您可以使用expl3
/ xparse
:
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{xparse}
\ExplSyntaxOn
% DEFINE BOX WITH GIVEN NAME
\NewDocumentCommand\mybox{m}
{
\clist_if_in:NnTF \g_api_mybox_labels_clist { #1 }
{
\group_begin:
\setlength\fboxsep{0pt}
\colorbox{teal}{\framebox(18,10){\bfseries\color{white}#1}}
\group_end:
}
{
\framebox(18,10){\bfseries#1}
}
}
\clist_new:N \g_api_mybox_labels_clist
\clist_gset:Nn \g_api_mybox_labels_clist { A , C }
\ExplSyntaxOff
\begin{document}
\begin{tcbitemize}[
size=fbox,
colframe=white,
colback=white,
raster equal height,
raster force size=false,
raster equal skip=0pt,
raster columns=4
]
\tcbitem[width=0.20\linewidth] \mybox{A}
\tcbitem[width=0.20\linewidth] \mybox{B}
\tcbitem[width=0.20\linewidth] \mybox{C}
\tcbitem[width=0.20\linewidth] \mybox{D}
\end{tcbitemize}
\end{document}
添加用于设置按键的界面很容易:
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{xparse}
\ExplSyntaxOn
% DEFINE BOX WITH GIVEN NAME
\NewDocumentCommand\mybox{m}
{
\clist_if_in:NnTF \g_api_mybox_labels_clist { #1 }
{
\group_begin:
\setlength\fboxsep{0pt}
\colorbox{teal}{\framebox(18,10){\bfseries\color{white}#1}}
\group_end:
}
{
\framebox(18,10){\bfseries#1}
}
}
\clist_new:N \g_api_mybox_labels_clist
\NewDocumentCommand{\setmyboxkeys}{m}
{
\clist_gset:Nn \g_api_mybox_labels_clist { #1 }
}
\ExplSyntaxOff
\begin{document}
\section{The list is not set}
\begin{tcbitemize}[
size=fbox,
colframe=white,
colback=white,
raster equal height,
raster force size=false,
raster equal skip=0pt,
raster columns=4
]
\tcbitem[width=0.20\linewidth] \mybox{A}
\tcbitem[width=0.20\linewidth] \mybox{B}
\tcbitem[width=0.20\linewidth] \mybox{C}
\tcbitem[width=0.20\linewidth] \mybox{D}
\end{tcbitemize}
\section{The list is set}
\setmyboxkeys{A,C}
\begin{tcbitemize}[
size=fbox,
colframe=white,
colback=white,
raster equal height,
raster force size=false,
raster equal skip=0pt,
raster columns=4
]
\tcbitem[width=0.20\linewidth] \mybox{A}
\tcbitem[width=0.20\linewidth] \mybox{B}
\tcbitem[width=0.20\linewidth] \mybox{C}
\tcbitem[width=0.20\linewidth] \mybox{D}
\end{tcbitemize}
\end{document}
您可能想要将键的选择添加到以下选项中tcbitemize
:
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{xparse}
\ExplSyntaxOn
% DEFINE BOX WITH GIVEN NAME
\NewDocumentCommand\mybox{m}
{
\clist_if_in:NnTF \l_api_mybox_labels_clist { #1 }
{
\group_begin:
\setlength\fboxsep{0pt}
\colorbox{teal}{\framebox(18,10){\bfseries\color{white}#1}}
\group_end:
}
{
\framebox(18,10){\bfseries#1}
}
}
\clist_new:N \l_api_mybox_labels_clist
\tcbset{mybox~keys/.code=\clist_set:Nn \l_api_mybox_labels_clist { #1 }}
\ExplSyntaxOff
\begin{document}
\section{The list is not set}
\begin{tcbitemize}[
size=fbox,
colframe=white,
colback=white,
raster equal height,
raster force size=false,
raster equal skip=0pt,
raster columns=4
]
\tcbitem[width=0.20\linewidth] \mybox{A}
\tcbitem[width=0.20\linewidth] \mybox{B}
\tcbitem[width=0.20\linewidth] \mybox{C}
\tcbitem[width=0.20\linewidth] \mybox{D}
\end{tcbitemize}
\section{The list is set}
\begin{tcbitemize}[
mybox keys={A,C},% <---- set the keys
size=fbox,
colframe=white,
colback=white,
raster equal height,
raster force size=false,
raster equal skip=0pt,
raster columns=4
]
\tcbitem[width=0.20\linewidth] \mybox{A}
\tcbitem[width=0.20\linewidth] \mybox{B}
\tcbitem[width=0.20\linewidth] \mybox{C}
\tcbitem[width=0.20\linewidth] \mybox{D}
\end{tcbitemize}
\end{document}
答案2
您可以使用 opmac.tex 中的四行代码,其中\isinlist
定义了宏:
\documentclass{standalone}
\usepackage[most]{tcolorbox}
% from opmac.tex:
\long\def\isinlist#1#2#3{\long\def\tmp##1#2##2\end{\def\tmp{##2}%
\ifx\tmp\empty \csname iffalse\expandafter\endcsname \else
\csname iftrue\expandafter\endcsname \fi}% end of \def\tmp
\expandafter\tmp#1\endlistsep#2\end
}
% DEFINE BOX WITH GIVEN NAME
\def\mybox#1{%
\isinlist\currentbox{#1}\iftrue
\setlength\fboxsep{0pt}\colorbox{teal}{\framebox(18,10)
{\textbf{\color{white}#1}}}%
\else
\framebox(18,10){\textbf{#1}}%
\fi
}
% define current box
\def\currentbox{AC}
\begin{document}
\begin{tcbitemize}[size=fbox,
colframe=white,
colback=white,
raster equal height,
raster force size=false,
raster equal skip=0pt,
raster columns=4]
\tcbitem[width=0.20\linewidth]
\mybox{A}
\tcbitem[width=0.20\linewidth]
\mybox{B}
\tcbitem[width=0.20\linewidth]
\mybox{C}
\tcbitem[width=0.20\linewidth]
\mybox{D}
\end{tcbitemize}%
\end{document}
请注意,您不再需要任何宏包。