Why can't I put a comma in a \newtcolorbox?
Just put the title with the comma into two more { }
, otherwise, I think tcolorbox
thinks there is another parameter.
\documentclass{article}
\usepackage[landscape]{geometry}
\usepackage[table]{xcolor}
\usepackage{mathtools}
\usepackage{multicol}
\usepackage{amssymb,amsthm}
\usepackage{lipsum}
\usepackage{minibox}
\usepackage[most]{tcolorbox}
\advance\topmargin-1in
\advance\textheight3in
\advance\textwidth3in
\advance\oddsidemargin-1.5in
\advance\evensidemargin-1.5in
\parindent0pt
\parskip2pt
\newcommand{\hr}{\centerline{\rule{3.5in}{1pt}}}
\begin{document}
\begin{multicols*}{3}
\newtcolorbox{mybox}[2][]{text width=0.97\textwidth,fontupper=\scriptsize,
fonttitle=\bfseries\sffamily\scriptsize, colbacktitle=black,enhanced,
attach boxed title to top left={yshift=-2mm,xshift=3mm},
boxed title style={sharp corners},top=3pt,bottom=2pt,
title=#2,colback=white}
%%------------ Angle, FM and PM and Narrowband FM ---------------
\hspace{-0.2cm}
\begin{minipage}{0.31\textwidth}
\begin{mybox}{{Angle, FM and PM and Narrowband FM}}
\lipsum[2]
\end{mybox}
\end{minipage}
\end{multicols*}
\end{document}
Brace the value for title
, so when the parameter is replaced by the actual title the comma will not be mistaken for a key-value separator.
\newtcolorbox{mybox}[2][]{
%text width=0.97\textwidth,%<--- avoid this
fontupper=\scriptsize,
fonttitle=\bfseries\sffamily\scriptsize,
colbacktitle=black,
enhanced,
attach boxed title to top left={yshift=-2mm,xshift=3mm},
boxed title style={sharp corners},
top=3pt,
bottom=2pt,
title={#2},% <------ Braces!
colback=white
}
Full example. I recommend avoiding to set text width
, so it will be computed automatically. I load lipsum
and showframe
just to show the effect.
\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum,showframe}
\newtcolorbox{mybox}[2][]{
fontupper=\scriptsize,
fonttitle=\bfseries\sffamily\scriptsize,
colbacktitle=black,
enhanced,
attach boxed title to top left={yshift=-2mm,xshift=3mm},
boxed title style={sharp corners},
top=3pt,
bottom=2pt,
title={#2},
colback=white
}
\begin{document}
%%------------ Angle, FM and PM and Narrowband FM ---------------
\begin{mybox}{Angle, FM and PM and Narrowband FM}
\lipsum*[2]
\end{mybox}
\end{document}
If you write \tcolorbox[title=Angle, FM, and PM and Narrowband FM,colback=white]
, when \tcolorbox
parses its bracketed argument, it looks for commas to separate options. It has no way to know that you intended FM
to still be part of the title, but colback
to be a separate option. You need to hide the non-option-separating comma inside a braced group: \tcolorbox[title={Angle, FM, and PM and Narrowband FM},colback=white]
. Braced groups are parsed before passing the argument to the command, therefore what tcolorbox
sees inside the square brackets is t
, i
, t
, l
, e
, =
, {Angle, FM, and PM and Narrowband FM},
,,
c,
o,
l`, … It's always ok to put the whole argument of an option inside braces.
The same applies with newtcolorbox
, with the added wrinkle that expanding #2
into the argument of the command happens before the argument parsing. There is no implicit group around #2
, so to cope with arbitrary arguments, you need to add braces.
\newtcolorbox{mybox}[2][]{ text width=0.97\textwidth, fontupper=\scriptsize, fonttitle=\bfseries\sffamily\scriptsize, colbacktitle=black, enhanced, attach boxed title to top left={yshift=-2mm,xshift=3mm}, boxed title style={sharp corners}, top=3pt, bottom=2pt, title={#2}, colback=white }
Note how the argument to attach boxed title to top left
uses braces for the same reason.
The braces should be around the whole argument to the title
option, not specifically around the argument of the command. For example, if you wanted to add a common prefix to all the titles, you should write
title={Joe's tips: #2},
There wouldn't be any difference in this particular case, but sometimes braces can have a subtle influence over spacing or over the action of some commands that define macros locally. The argument of an option becomes a braced group deep under the hood, so putting braces around it doesn't change anything, whereas building additional braced groups over part of the argument can change things.
The same principle applies generally with many commands. It's a low-level TeX thing.