12

When I try to put a "," in the mybox environment, I get the error:

! Package pgfkeys Error: I do not know the key '/tcb/FM and PM and Narrowband F M' and I am going to ignore it. Perhaps you misspelled it.

Is there a way to put a comma "," in the tcolorbox header so that my header appears as: Angle, FM, and PM and Narrowband FM?

CODE

\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}
  • Gilles's answer is the best, you can change the accepted answer. – CarLaTeX Oct 31 '18 at 15:35
10

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}

enter image description here

10

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}

enter image description here

5

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.

  • +1 for explaining the actual problem in detail :) But, although the observed behavior makes sense from a technical (or TeXnical as it is sometimes called) perspective, it is rather counterintuitive for the user - you would expect that tcolorbox (in this case \newtcolorbox) would try to hide such implementation issues from the user if possible. – Marijn Oct 31 '18 at 15:21
  • @Marijn Unfortunately that's not possible without major changes to TeX which would break all the TeX and LaTeX code out there. TeX wasn't designed in a way that makes it easy to add functionality such as named arguments to macro. – Gilles Oct 31 '18 at 19:16

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.