`\newtcolorbox` 的 3 或 4 个参数

`\newtcolorbox` 的 3 或 4 个参数

这个问题如下https://tex.stackexchange.com/a/688116/298558

在这里,我问了一个关于 \newtcolorbox 接受参数的问题。手册第 15 页有一个接受 2 个参数的示例:

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

我想向 \newtcolorbox 传递 3 或 4 个参数,以便它采用以下形式:

\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{mybox}[colback=yellow]{Hello there}{0.3\textheight}{flush center}    This is my own box with a mandatory title   and options. \end{mybox}

这不起作用:

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

如果有人能给我一些想法,我会很高兴。

答案1

你在问题中没有提供例子,但在评论中你展示了

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

产生

! 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
?

请注意显示已读取后的换行符T。它是命令的第 4 个参数,因此会产生halign={T}并显示错误。

你可以使用

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

但混合使用 keyval 和位置参数是一种反模式,我会使用

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

相关内容