0

This question follows https://tex.stackexchange.com/a/688116/298558 .

Here, I asked a question about \newtcolorbox taking agruments. On page 15 on the manual there is an example that takes 2 arguments:

\newtcolorbox{mybox}[2][]{colback=red!5!white, colframe=red!75!black,fonttitle=\bfseries,
colbacktitle=red!85!black,enhanced, attach boxed title to top center={yshift=-2mm},
title={#2},#1}
\begin{mybox}[colback=yellow]{Hello there}
This is my own box with a mandatory title
and options.
\end{mybox}

I would like to pass 3 or 4 arguments to \newtcolorbox so that it takes the following form:

\newtcolorbox{mybox}[4][]{colback=red!5!white, colframe=red!75!black,fonttitle=\bfseries,
colbacktitle=red!85!black,enhanced, attach boxed title to top center={yshift=-2mm},
title={#2}, height = #3, halign = #4, #1}

This works:

\begin{mybox}[colback=yellow]{Hello there}{0.3\textheight}{flush center}    This is my own box with a mandatory title   and options. \end{mybox}

This doesn't work:

\begin{mybox}[colback=yellow, halign = flush left]{Hello there}{0.3\textheight} 

I would be pleased, if someone could give me ideas.

8
  • Write [4] instead of [3]. Commented Jun 13, 2023 at 19:28
  • Thanks, but this doesn't function :)
    – Kerry
    Commented Jun 13, 2023 at 19:48
  • if you show no example or error message it is hard to help Commented Jun 13, 2023 at 19:51
  • \newtcolorbox{mybox}[4][]{colback=red!5!white, colframe=red!75!black,fonttitle=\bfseries, colbacktitle=red!85!black,enhanced, attach boxed title to top center={yshift=-2mm}, title={#2}, height = {#3}, halign = {#4}, #1} Then, I use \begin{mybox}[colback=yellow]{Hello there}{0.3\textheight} This is my own box with a mandatory title and options. \end{mybox} I get the error: "Package pgfkeys Error: Choice 'T' unknown in choice key '/tcb/halign'. I am going to ignore this key. T"
    – Kerry
    Commented Jun 13, 2023 at 19:59
  • 1
    edit the question putting code in comments does not work well, but [4][]{ defines a command with one optional and three mandatory arguments so #4 is T in your example and the body starts his is my own Commented Jun 13, 2023 at 20:05

1 Answer 1

2

You provided no example in the question but in comments you show

\documentclass{article}
\usepackage[many]{tcolorbox}
\newtcolorbox{mybox}[4][]{%
    colback=red!5!white, colframe=red!75!black,fonttitle=\bfseries,
    colbacktitle=red!85!black,enhanced, attach boxed title to top center={yshift=-2mm},
    title={#2}, height = {#3}, halign = {#4}, #1}
\begin{document}
 
\begin{mybox}[colback=yellow]{Hello there}{0.3\textheight}
    This is my own box with a mandatory title
    and options.
\end{mybox} 
\end{document}

which produces

! Package pgfkeys Error: Choice 'T' unknown in choice key '/tcb/halign'. I am g
oing to ignore this key.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.10    T
        his is my own box with a mandatory title
?

Note the linebreak after T showing this has been read. It is the 4th argument of your command, so produce halign={T} and the error shown.

You could use

\documentclass{article}
\usepackage[many]{tcolorbox}
\newtcolorbox{mybox}[4][]{%
    colback=red!5!white, colframe=red!75!black,fonttitle=\bfseries,
    colbacktitle=red!85!black,enhanced, attach boxed title to top center={yshift=-2mm},
    title={#2}, height = {#3}, halign = {#4}, #1}
\begin{document}
 
\begin{mybox}[colback=yellow]{Hello there}{0.3\textheight}{left}
    This is my own box with a mandatory title
    and options.
\end{mybox} 
\end{document}

But mixing keyval and positional arguments is an anti-pattern, I would use

\documentclass{article}
\usepackage[many]{tcolorbox}
\newtcolorbox{mybox}[2][]{%
    colback=red!5!white, colframe=red!75!black,fonttitle=\bfseries,
    colbacktitle=red!85!black,enhanced, attach boxed title to top center={yshift=-2mm},
    title={#2}, #1}
\begin{document}
 
\begin{mybox}[colback=yellow, height = 0.3\textheight, halign =left]{Hello there}
    This is my own box with a mandatory title
    and options.
\end{mybox} 
\end{document}
1
  • Both suggestions work well. Thank you very much !!!!
    – Kerry
    Commented Jun 13, 2023 at 20:46

You must log in to answer this question.

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