创建包含 itemize 和 tcolorbox 的环境

创建包含 itemize 和 tcolorbox 的环境

考虑以下 MWE

\documentclass{article}
\usepackage{tikz,lipsum,lmodern}
\usepackage[most]{tcolorbox}
\usepackage{blindtext}

% https://tex.stackexchange.com/questions/350316/create-manual-page-break-within-tcolorbox
\newtcolorbox{ExampleBox}{%
    breakable,
    enhanced,
    colback=black!10!white,
    colframe=black!25!white,
    coltitle=black,
    title=\textbf{Example}
}
\newtcolorbox{NoteBox}{%
    breakable,
    enhanced,
    colback=red!10!white,
    colframe=red!25!white,
    coltitle=black,
    title=\textbf{Note}
}

\begin{document}


\begin{ExampleBox}
    \begin{itemize}
        \item Ich gehe nach Hause. \textit{I'm going home.}\\ 
        \blindtext[1]
        \item Ich gehe nach Hause. \textit{I'm going home.}
    \end{itemize} 
\end{ExampleBox}
\begin{NoteBox}
    \begin{itemize}
        \item Ich gehe nach Hause. \textit{I'm going home.}\\ 
        \blindtext[1]
    \end{itemize} 
\end{NoteBox}

\end{document}

这将创建两个新的tcolorboxes名为ExampleBoxNoteBox。现在,在我的原始文档中,我又有了几个这样的框,它们的内容始终是相同的形式:一个itemize环境,每个框\item后面跟着一些文本,然后是一些草书文本,然后是一个选修的 \\ + some more text

这种方法给了我想要的结果,但需要大量(希望如此)不必要的编写。所以我想知道是否可以创建一个这样的环境

\begin{environment}[ex]
    \item{first sentence}{cursive sentence}{optional text}
    \item{first sentence}{cursive sentence}
\end{environment}

其结果应与上述相同ExampleBox。我们的想法是在某处(以某种形式)定义形式为 (tag, Title, color) 的三元组 --- 在上述情况下为 (ex, Example, black) --- 以使生成和使用这种非常具体的代码变得更tcolorboxes容易一些。


不确定是否相关,但我使用 LuaLaTeX,并且first sentence修改后的环境中\item会包含特殊字符(如汉字或西里尔字母)。

答案1

这是我的尝试。框的颜色和标题根据新环境的参数自动确定MyItemBox。请注意,我没有在 custom \item-like 命令中包含第三个可选参数。

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

\usepackage{xstring}     % Used for \IfEqCase


\newcommand{\myboxtitle}{default}     % Default box title
\definecolor{myboxcolor}{named}{blue} % Default box color
\newtcolorbox{MyBox}[1]{%
    breakable,
    enhanced,
    colback=myboxcolor!10!white,      % Box color is used here
    colframe=myboxcolor!25!white,     % Box color is used here
    coltitle=black,
    title=\textbf{\myboxtitle}        % Box title is used here 
}

\newenvironment{MyItemBox}[1]{\IfEqCase{#1}{%                                % Code at the beginning of the environment
                             {ex}{\definecolor{myboxcolor}{named}{black}%    % Define color and title for argument = ex
                                  \renewcommand{\myboxtitle}{Example}}%
                             {note}{\definecolor{myboxcolor}{named}{red}%    % Define color and title for argument = note
                                    \renewcommand{\myboxtitle}{Note}}%
                             }\begin{MyBox}{#1}%
                             \begin{itemize}}%
                             {\end{itemize}%                                 % Code at the ned of the environment
                              \end{MyBox}}

\newcommand{\myitem}[2]{\item #1 \textit{#2}}      % Custom itemize command. Second argument is italics



\begin{document}

\begin{MyItemBox}{note}
\myitem{Ich gehe nach Hause.}{I'm going home.}\\ 
\blindtext[1]
\myitem{Ich gehe nach Hause.}{I'm going home.}
\end{MyItemBox}

\begin{MyItemBox}{ex}
\myitem{Ich gehe nach Hause.}{I'm going home.}
\myitem{Ich gehe nach Hause.}{I'm going home.}
\end{MyItemBox}

\begin{MyItemBox}{}
\myitem{Ich gehe nach Hause.}{I'm going home.}
\myitem{Ich gehe nach Hause.}{I'm going home.}
\end{MyItemBox}



\end{document}

答案2

\myitem基于包的命令命题xparse\IfValueT{#3}{\\ #3}检查是否给出了可选文本。

\documentclass{article}
\usepackage{tikz,lipsum,lmodern}
\usepackage[most]{tcolorbox}
\usepackage{blindtext}
\usepackage{xparse}

% https://tex.stackexchange.com/questions/350316/create-manual-page-break-within-tcolorbox
\newtcolorbox{ExampleBox}{%
    breakable,
    enhanced,
    colback=black!10!white,
    colframe=black!25!white,
    coltitle=black,
    title=\textbf{Example}
}
\newtcolorbox{NoteBox}{%
    breakable,
    enhanced,
    colback=red!10!white,
    colframe=red!25!white,
    coltitle=black,
    title=\textbf{Note}
}

\NewDocumentCommand{\myitem}{ m m o }{
\item #1 \textit{#2} \IfValueT{#3}{\\ #3}
}
% New command with 2 mandatory and one optional arguments
% With a value check on optional argument

\begin{document}


\begin{ExampleBox}
    \begin{itemize}
        \myitem{Ich gehe nach Hause.}{I'm going home.}[\blindtext[1]]
         \myitem{Ich gehe nach Hause.}{I'm going home.}
    \end{itemize} 
\end{ExampleBox}
\begin{NoteBox}
    \begin{itemize}
        \myitem{Ich gehe nach Hause.}{I'm going home.}[\blindtext[1]]
    \end{itemize} 
\end{NoteBox}

\end{document}

编辑利用库tcolorbox的轻微变体skins

\documentclass{article}
\usepackage{tikz,lipsum,lmodern}
\usepackage[most]{tcolorbox}
\tcbuselibrary{skins}
\usepackage{blindtext}
\usepackage{xparse}

\newenvironment{ExampleBox}{\begin{itemize}}{\end{itemize}}
\tcolorboxenvironment{ExampleBox}{%
    breakable,
    enhanced,
    colback=black!10!white,
    colframe=black!25!white,
    coltitle=black,
    title=\textbf{Example}
}
\newenvironment{NoteBox}{\begin{itemize}}{\end{itemize}}
\tcolorboxenvironment{NoteBox}{%
    breakable,
    enhanced,
    colback=red!10!white,
    colframe=red!25!white,
    coltitle=black,
    title=\textbf{Note}
}

\NewDocumentCommand{\myitem}{ m m o }{
\item #1 \textit{#2} \IfValueT{#3}{\\ #3}
}
% New command with 2 mandatory and one optional arguments
% With a value check on optional argument

\begin{document}
\begin{ExampleBox}
        \myitem{Ich gehe nach Hause.}{I'm going home.}[\blindtext[1]]
         \myitem{Ich gehe nach Hause.}{I'm going home.}
\end{ExampleBox}
\begin{NoteBox}
        \myitem{Ich gehe nach Hause.}{I'm going home.}[\blindtext[1]]
\end{NoteBox}
\end{document}

在此处输入图片描述

相关内容