T-F-S / tcolorbox

A LaTeX package to create highly customizable colored boxes.
http://www.ctan.org/pkg/tcolorbox
LaTeX Project Public License v1.3c
213 stars 15 forks source link

Strange Key Names #271

Closed Addlai closed 4 months ago

Addlai commented 4 months ago

When I took my first computer programming course, in 1980, we were instructed not to use short or cryptic identifiers, and points were deducted if we did. I've remained with this bias ever since.

LaTeX and TeX, or course, are notorious for the ineptitude of their programming model, which among other things, places very weird and awkward limitations on identifiers. One of the things I really like about Tikz is that it does not place such weird limitations on identifiers, but freely allows and encourages intelligible identifiers that use full words, spaces, etc.

Because of my biases, I have a hard time keeping identifiers like colupper, colback, colbacktitle, leftupper, top, ... straight, and I prefer not to have to expend time and mental energy looking up such things and deciphering what is wrong when I get unexpected results.

Is there a way to create custom global aliases for tcolorbox's keywords, so I can rename some of the ones I find confusing or hard to remember?

muzimuzhi commented 4 months ago

Possible with fancy pgfkeys (unofficial online doc) key handlers, though may cause efficiency concerns.

\documentclass{article}
\usepackage{tcolorbox}

% make aliases under another key prefix
\pgfkeys{
  /mytcbalias/.is family,
  % if "/tcb/<key>" is unknown, try "/mytcbalias/<key>"
  /tcb/.search also={/mytcbalias}
}

\pgfqkeys{/mytcbalias}{
  upper part.color/.style={/tcb/colupper={#1}},
  upper part.font/.style={/tcb/fontupper={#1}}
}

\begin{document}
\begin{tcolorbox}[colupper=cyan, fontupper=\Large\sffamily]
  content
\end{tcolorbox}

\begin{tcolorbox}[upper part.color=cyan, upper part.font=\Large\sffamily]
  content
\end{tcolorbox}
\end{document}

image

PS: Like /tcb keys, these alias are defined locally, but as far as they are defined at top level and before any use of tcolorboxes...

T-F-S commented 4 months ago

Possible with fancy pgfkeys (unofficial online doc) key handlers

Best possible answer!

Addlai commented 4 months ago

That's an elegant solution. Thanks!

Addlai commented 4 months ago

Actually, a problem came up with trying to rename colbacktitle using ... title.color.background/.style={/tcb/colbacktitle={#1}}, ... which results in the error message: ERROR: Package pgfkeys Error: I do not know the key '/mytcbalias/title filled' and I am going to ignore it.

I think it has something to do with the /tcb/title filled key, whose documentation states, "... This option is set to true automatically by /tcb/colbacktitle, /tcb/opacitybacktitle, and /tcb/title style, and /tcb/title code. ..." I suspect the alias mechanism isn't compatible with the way tcolorbox sets this variable, but I don't know.

muzimuzhi commented 4 months ago

Hmm the tricky problem of current default path when too many pgfkeys features are used together. 😅

Adding /mytcbalias/.search also={/tcb} should work.

Revised example (v2)

\documentclass{article}
\usepackage{tcolorbox}

% make aliases under another key prefix
\pgfkeys{
  /mytcbalias/.is family,
  % if "/tcb/<key>" is unknown, try "/mytcbalias/<key>"
  /tcb/.search also={/mytcbalias},
  % If "/mytcbalias/<key>" is unknown, try "/tcb/<key>"; if "/tcb/<key>" is
  % again unknown (no recursive ".search also" involved), throw general error
  % about unknown key.
  %
  % Mutual "/.search also" is needed to support aliased "/tcb" style keys, like
  %     /tcb/colbacktitle/.style={{title filled,@colbacktitle={#1}}}
  /mytcbalias/.search also={/tcb}
}

\pgfqkeys{/mytcbalias}{
  upper part.color/.style={/tcb/colupper={#1}},
  upper part.font/.style={/tcb/fontupper={#1}},
  title.color.background/.style={/tcb/colbacktitle={#1}},
}

\begin{document}
\begin{tcolorbox}[
  title=Using original tcolorbox options,
  colupper=cyan,
  fontupper=\Large\sffamily,
  colbacktitle=gray!80
]
  content
\end{tcolorbox}

\begin{tcolorbox}[
  title=Using aliased options,
  upper part.color=cyan,
  upper part.font=\Large\sffamily,
  title.color.background=gray!80
]
  content
\end{tcolorbox}

% should throw error
\begin{tcolorbox}[
  title={Testing unknown option, should throw error},
  key unknown to both prefixes
]
  content
\end{tcolorbox}
\end{document}

image