3

I realized that if you put the length here:

attach boxed title to top left={yshift=<length>}

in pt, mm or cm, the title box is shifted.

If you put it in ex or em, it is not.

Is it a tcolorbox bug?

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{mwe}

\begin{document}

\begin{tcolorbox}[title=My title, 
    enhanced,
    attach boxed title to top left={yshift=-40pt},
    ]
    With \texttt{pt} (or \texttt{mm}, or \texttt{cm}) the positioning works
\end{tcolorbox}

\vspace{30pt}

\begin{tcolorbox}[title=My title, 
    enhanced,
    attach boxed title to top left={yshift=-4ex},
    ]
    With \texttt{ex} (or \texttt{em}) it doesn't work
\end{tcolorbox}

\end{document}

enter image description here

CarLaTeX
  • 62,716

2 Answers2

5

It works it's just not that useful.

em is a font specific length and without tracing all the code you can take a guess of the font in force at that point if you add

\def\nullfont{\ERROR}

before the box then you get

! Undefined control sequence.
\nullfont ->\ERROR 

l.11 

which isn't that useful other than it tells you that \nullfont is being used.

\nullfont is a built in font with no characters and 1em is 0pt....

David Carlisle
  • 757,742
  • it is in \pgf@picture. Patching it to remove the \nullfont makes the yshift work, but there is extra horizontal shift (of the whole thing and even more of the title) presumably from space tokens. –  May 01 '18 at 10:12
  • Ah, so it's like setting 0pt, but why does it normally work in tikz? – CarLaTeX May 01 '18 at 10:13
  • @CarLaTeX I refer you to the last sentence of https://tex.stackexchange.com/a/97556/1090 – David Carlisle May 01 '18 at 10:14
  • @DavidCarlisle trying to squeeze some extra rep by posting that link? – Skillmon May 01 '18 at 11:16
  • 1
    It doesn't work in normal tikz either (for the same reason). Try e.g. xxx \tikz{\coordinate(a) at (0,0);\draw[green] ([yshift=4mm]a)--++(2,0);\draw[red] ([yshift=4em]a)--++(2,0);}. It works inside nodes as there a font is active. – Ulrike Fischer May 01 '18 at 11:46
1

Just for fun: the following works. But depending on the order things are executed in the entrails of tcolorbox and tikz, it may fail in other cases.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{mwe}

\newdimen\myex
\newcommand*{\getex}{\global\myex=1ex}

\begin{document}

\begin{tcolorbox}[title=My title\getex, 
    enhanced,
    attach boxed title to top left={yshift=-10.2\myex},
    ]
    With \texttt{1ex} stored in the title it works
\end{tcolorbox}

\vspace{30pt}

\begin{tcolorbox}[title=My title, 
    enhanced,
    attach boxed title to top left={yshift=-10.2\myex},
    ]
    \getex With \texttt{1ex} stored in the text it works
\end{tcolorbox}

\end{document}

enter image description here

Mike
  • 8,664