0

I have these two new commands defined:

\usepackage{tcolorbox}

\newcommand{\mycommand}[1]{
    \begin{tcolorbox}[
        center, colback=black!20!white,
        width=.75\textwidth, boxrule=0pt, frame empty]

        #1
    \end{tcolorbox}
}

\newcommand{\othercommand}[1]{
    \begin{tcolorbox}[
        width=.75\textwidth, boxrule=0pt, frame empty]

        #1
    \end{tcolorbox}
}

As you can see, both commands are almost identical, apart from the fact that the first one has center and colback arguments being passed. Is there a way I could generalize these two commands into a single command that can take an extra argument, and depending on whether that argument is present on the invocation, render either of the results?

2 Answers 2

2

You can take advantage of the fact that \tcbset applies to all tcolorboxes in the same group level.

\documentclass{article}
\usepackage{tcolorbox}

\NewDocumentCommand{\mycommand}{sm}{%
  \begingroup
  \IfBooleanT{#1}{\tcbset{center,colback=black!20!white}}%
  \begin{tcolorbox}[
    width=.75\textwidth,
    boxrule=0pt,
    frame empty
  ]
  #2
  \end{tcolorbox}%
  \endgroup
}

\begin{document}

\noindent X\dotfill X

\mycommand{
  something something something something
  something something something something
  something something something something
}

\mycommand*{
  something something something something
  something something something something
  something something something something
}

\mycommand{
  something something something something
  something something something something
  something something something something
}

\end{document}

The *-variant applies center and colback.

enter image description here

2
  • +1: Is \IfBooleanT{#1} responsible for the * version? And #2 is the actual argument of the command \mycommand? Jul 23, 2022 at 22:08
  • 2
    @Dr.ManuelKuehner The arguments are specified as sm. With s we denote a possible *; if there is at call time, \IfBooleanTF{#1}{<true>}{<false>} will return the <true> text, otherwise the <false> text. When there is no <false> text, we can abbreviate with \IfBooleanT{#1}{<true>}. With m a mandatory argument is specified.
    – egreg
    Jul 23, 2022 at 22:14
1

A variation of egreg's answer which takes into account that square brackets of optional arguments don't need to be matched when occurring within undelimited arguments:

\documentclass{article}
\usepackage{tcolorbox}

\NewDocumentCommand{\mycommand}{sm}{%
    \IfBooleanTF{#1}{\begin{tcolorbox}[center, colback=black!20!white, }%
                    {\begin{tcolorbox}[}%
        width=.75\textwidth, boxrule=0pt, frame empty]%
    #2%
    \end{tcolorbox}%
}%

\begin{document}

\noindent X\dotfill X

\mycommand{
  something something something something
  something something something something
  something something something something
}

\mycommand*{
  something something something something
  something something something something
  something something something something
}

\mycommand{
  something something something something
  something something something something
  something something something something
}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .