2

I couldn't find a way to make the tcolorbox adjust automatically with the tikzpictures inside or the title (if it's wider). I also want them to appear on the same line.

Here's my MWE

\documentclass[a4paper,11pt]{article}
\usepackage[french]{babel}
 \usepackage[utf8]{inputenc}
 \usepackage[T1]{fontenc}
 \usepackage{tikz}
 \usepackage{tcolorbox}

\begin{document}

\newtcolorbox{blueboxb}[1]{hbox,center title, colback=pink!20, colframe=blue!20,fonttitle=\bfseries,title={#1}} \newtcolorbox{blueboxs}[1]{hbox,center title, colback=pink!20, colframe=blue!20,fonttitle=\bfseries,title={#1}} \newtcolorbox{orangebox}[1]{colback=pink!20,colframe=orange!40,fonttitle=\bfseries, title={#1}}

\begin{blueboxb}{\color{black}{Droites parallèles}} \begin{tikzpicture}[scale=0.5] %1 \begin{scope} \draw (0,0) -- (5,0) -- (7,3) -- (2,3) -- cycle; \draw[green!50!blue] (2,1) -- (4.5,2.5) node [right] {$\Delta$}; \draw[red!80,xshift=0.5cm,yshift=-0.5cm] (2,1) -- (4.5,2.5) node [right] {$\Delta^{\prime}$}; \end{scope}

%2 \begin{scope}[xshift=8cm] \draw (0,0) -- (5,0) -- (7,3) -- (2,3) -- cycle; \draw[red!80] (2,2.5) -- (5.5,1.5); \draw[green!50!blue,dashed,thick] (2,2.5) -- (5.5,1.5) node [midway,above right] {$\Delta=\Delta^{\prime}$}; \end{scope} \end{tikzpicture} \end{blueboxb} \begin{blueboxs}{\color{black}{Droites Confondues}} \begin{tikzpicture}[scale=0.5] \draw (0,0) -- (5,0) -- (7,3) -- (2,3) -- cycle; \draw[red!80] (2,2.5) -- (4.5,1) node [right] {$\Delta$}; \draw[green!50!blue] (2,1) -- (4.5,2.5) node [right] {$\Delta^{\prime}$}; \end{tikzpicture} \end{blueboxs} \begin{orangebox}{\color{black}{Droites Confondues}} \begin{tikzpicture}[scale=0.5] \draw (0,0) -- (5,0) -- (7,3) -- (2,3) -- cycle; \draw[thick,red!80] (2.5,4.5) node[black,right] {$\Delta^{\prime}$} -- (5,-0.5); \fill[pink!20] (4,0.1) rectangle (4.9,2); \draw[red!80,dashed] (2.5,4.5) -- (5,-0.5); \draw[green!50!blue] (0.5,0.4) node [right,black] {$\Delta$} -- (3,2.5); \end{tikzpicture} \end{orangebox}

\end{document}

Thank you!

  • 1
    Can this: https://tex.stackexchange.com/questions/445992/adjusting-the-width-of-a-tcolorbox-to-its-content help? – Rmano Mar 15 '22 at 21:10
  • Thank you @Rmano it does help. Is there a way to adjust with the title length when it is wider than the content? I know this is not what I asked in the beginning. Also how to put the 3 on the same line? – stargazer45 Mar 15 '22 at 21:13
  • I do not think that there is an option for taking into account the tile length, although you can always add some phantom line as width as the title in the boxes. To put three of them, if they fit (including the spaces between) they just will go on the same line. – Rmano Mar 15 '22 at 21:44
  • @Rmano they fit, or at least 2 of them fit, but I have one in each line (so 3 lines) – stargazer45 Mar 15 '22 at 21:49
  • ...I'm not on the computer now, I'll check tomorrow... – Rmano Mar 15 '22 at 21:52
  • 1
    If all your boxes will contain tikzpictures you can use a tikz upper option and save declaring begin{tikzpicture}...\end{tikzpicture} into every box. And if you add coltitle=black you can avoid fixing the title color for all of them. – Ignasi Mar 16 '22 at 09:17
  • Thanks a lot! that was helpful – stargazer45 Mar 18 '22 at 08:02

1 Answers1

1

As you can see in the linked answer (a bit hidden, but...)

  1. The hbox keyword makes the box the (horizontal) size of the content, and
  2. the nobeforeafter switch suppresses the normal spacing that tcolorbox issues before and after the box (look for it in the manual).

Adding this option, and showing the frame of the page, you have:

boxes

Notice the \parident at the start of the first line, and the fact that you do not have hbox nor nobeforeafter on the orangebox, so it starts with something similar to \par\noindent`.

Code for this:

documentclass[a4paper,11pt]{article}
\usepackage[french]{babel}
 \usepackage[utf8]{inputenc}
 \usepackage[T1]{fontenc}
 \usepackage{tikz}
 \usepackage{tcolorbox}
 \usepackage[showframe]{geometry}

\begin{document}

\newtcolorbox{blueboxb}[1]{hbox,center title, colback=pink!20, colframe=blue!20,fonttitle=\bfseries,title={#1},nobeforeafter} \newtcolorbox{blueboxs}[1]{hbox,center title, colback=pink!20, colframe=blue!20,fonttitle=\bfseries,title={#1},nobeforeafter} \newtcolorbox{orangebox}[1]{colback=pink!20,colframe=orange!40,fonttitle=\bfseries, title={#1}}

\begin{blueboxb}{\color{black}{Droites parallèles}} \begin{tikzpicture}[scale=0.5] %1 \begin{scope} \draw (0,0) -- (5,0) -- (7,3) -- (2,3) -- cycle; \draw[green!50!blue] (2,1) -- (4.5,2.5) node [right] {$\Delta$}; \draw[red!80,xshift=0.5cm,yshift=-0.5cm] (2,1) -- (4.5,2.5) node [right] {$\Delta^{\prime}$}; \end{scope} %2 \begin{scope}[xshift=8cm] \draw (0,0) -- (5,0) -- (7,3) -- (2,3) -- cycle; \draw[red!80] (2,2.5) -- (5.5,1.5); \draw[green!50!blue,dashed,thick] (2,2.5) -- (5.5,1.5) node [midway,above right] {$\Delta=\Delta^{\prime}$}; \end{scope} \end{tikzpicture} \end{blueboxb} \begin{blueboxs}{\color{black}{Droites Confondues}} \begin{tikzpicture}[scale=0.5] \draw (0,0) -- (5,0) -- (7,3) -- (2,3) -- cycle; \draw[red!80] (2,2.5) -- (4.5,1) node [right] {$\Delta$}; \draw[green!50!blue] (2,1) -- (4.5,2.5) node [right] {$\Delta^{\prime}$}; \end{tikzpicture} \end{blueboxs} \begin{orangebox}{\color{black}{Droites Confondues}} \begin{tikzpicture}[scale=0.5] \draw (0,0) -- (5,0) -- (7,3) -- (2,3) -- cycle; \draw[thick,red!80] (2.5,4.5) node[black,right] {$\Delta^{\prime}$} -- (5,-0.5); \fill[pink!20] (4,0.1) rectangle (4.9,2); \draw[red!80,dashed] (2.5,4.5) -- (5,-0.5); \draw[green!50!blue] (0.5,0.4) node [right,black] {$\Delta$} -- (3,2.5); \end{tikzpicture} \end{orangebox} \end{document}

If you add hbox and nobeforeafter to the orangebox, you'll have:

overfull boxes

...because they do not fit in a line.

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • 1
    Awesome, thank you @Rmano. I found another trick to align them, it's to put them in minipages (also a hspace to force them to fit on a line). Thanks again for your helps! – stargazer45 Mar 18 '22 at 08:04