3

Consider the following 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}

This creates two new tcolorboxes named ExampleBox and NoteBox. Now in my original document I have several more of these boxes and their content is always of the same form: an itemize environment, where each \item is followed by some text, then some cursive text and then an optional \\ + some more text.

This approach gives me the desired result but involves a lot of (hopefully) unnecessary writing. So I was wondering if it is possible to create an environment of the form

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

which should produce the same result as the above ExampleBox. The idea would be to define somewhere (in some form) triplets of the form (tag, Title, color) --- which would be in the above case (ex, Example, black) --- to make the generation and using of such very specific tcolorboxes a bit easier.


Not sure if relevant, but I use LuaLaTeX and the first sentence in the modified \item environment would contain special characters (like Kanji or Cyrillic letters).

3

Here is my attempt. The color and title of the box are automatically determined depending on the argument gien to the new MyItemBox environment. Please note that I did not include a third optional argument to the custom \item-like command.

\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}
3

A proposition of \myitem command, based on the xparse package. The \IfValueT{#3}{\\ #3} checks if an optional text is given or not.

\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}

EDIT Slight variant taking advantage of tcolorbox's skins library

\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}

enter image description here

  • Maybe you should add the environment using the tcolorbox and the itemize. – projetmbc Sep 8 at 20:06
  • 1
    @projetmbc I added a slight improvement, thanks for the heads up – BambOo Sep 8 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.