3
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}

\newtcolorbox{mybox}[2][]{
  enhanced,
  attach boxed title to top left={yshift=-2ex,xshift=4ex}, % NO EFFECT !!!
  colframe=black,
  colback=white,
  fonttitle=\bfseries,
  colbacktitle=white,
  coltitle=black,
  boxed title style={
    boxrule=0pt,
    colframe=white,
    },
  title=#2,
  #1}

\setlength\parindent{0pt}
\pagestyle{empty}
\begin{document}

\begin{mybox}{hello world}
  this is a test
\end{mybox}

\end{document}

the output pdf:

enter image description here

I don't know the reason why the title is above

9
  • Welcome to TeX.SX! I can't reproduce your image with your code. The shifting works well and hello world is placed on the line.
    – Bobyandbob
    Sep 7, 2017 at 15:00
  • 1
    You should not use ex length units here. Try mm or pt instead
    – user31729
    Sep 7, 2017 at 15:07
  • 4
    The examples in the tcolorbox manual never use ex units, only mm, cm or pt are applied. The ex specification is disregarded either by design or it is a bug
    – user31729
    Sep 7, 2017 at 15:29
  • 3
    Since ex is font dependent, it might not make any sense in tikz (which is what tcolorbox uses). In some of its constructions tikz does something strange to the current font (making it nullfont?) and thus things like ex and em might simply not be defined at the to imellem of use.
    – daleif
    Sep 7, 2017 at 16:37
  • 1
    @daleif Correct. From manual chapter 12.2: "Next, the contents of the environment is processed and the graphic commands therein are put into a box. Non-graphic text is suppressed as well as possible, but non-pgf commands inside a {tikzpicture} environment should not produce any “output” since this may totally scramble the positioning system of the backend drivers. The suppressing of normal text, by the way, is done by temporarily switching the font to \nullfont. You can, however, “escape back” to normal TeX typesetting. This happens, for example, when you specify a node." Sep 7, 2017 at 20:46

1 Answer 1

3

Using a CW answer here as a combinations of the comments by daleif, Torbjørn T and myself.

None of the examples in the tcolorbox manual uses the ex dimension, only cm, pt or mm are used.

attach boxed title to top left uses TikZ in the background, this in turn uses the \nullfont in order to suppress (see the tcolorbox : attach boxed title has no effect comment), as such ex has no effect as a dimension specification.

Thanks to daleif and Torbjørn T. for their useful comments -- those posts were incorporated here in short.

Back to the code itself -- shifting could be done with some reasonable values, such as \tcboxedtitleheight/2, for example.

\documentclass{article}
\usepackage[skins]{tcolorbox}

\newtcolorbox{mybox}[2][]{
  enhanced,
  attach boxed title to top left={yshift=\tcboxedtitleheight/2},
  colframe=black,
  colback=white,
  fonttitle=\bfseries,
  colbacktitle=white,
  coltitle=black,
  boxed title style={
    frame hidden,
  },
  title=#2,
  #1
}

\setlength{\parindent}{0em}
\pagestyle{empty}
\begin{document}

\begin{mybox}{hello world}
  this is a test
\end{mybox}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .