5

I want to embed tikzcircuits in tcolorboxes in a document; I am struggling to find a way of making the box behaving more or less like an fbox with respect to the width.

I am solving the problem by setting the width by hand (see code below), but it would be nice to have a way to tell tcolorbox to make a box as wide as needed. Is it possible?

This is the MWE:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbset{common/.style={
        colback=yellow!10,
        colframe=blue!60,
        halign=flush left, leftrule=4mm,
}}
\newtcolorbox{circbox}[3][]{common,
    width=#2\linewidth, title = {#3}, nobeforeafter, 
    #1}
\begin{document}
    \begin{circbox}{0.3}{Polarización}
        \begin{tikzpicture}
            \draw[thick] (0,0) circle (1cm);
        \end{tikzpicture}
    \end{circbox}
    \begin{circbox}{0.5}{Señal}
        \begin{tikzpicture}
            \draw[thick] (0,0) circle (1cm) (1.3,0) circle (1cm);
        \end{tikzpicture}
    \end{circbox}

    \bigskip

    \fbox{
        \begin{tikzpicture}
            \draw[thick] (0,0) circle (1cm);
        \end{tikzpicture}
    }
    \fbox{
        \begin{tikzpicture}
            \draw[thick] (0,0) circle (1cm) (1.3,0) circle (1cm);
        \end{tikzpicture}
    }
\end{document}

which, compiled, led to:

Output of the previous code

7

There's an option called hbox which basically does what you want, adjust the width to the content.

hbox

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbset{common/.style={
        colback=yellow!10,
        colframe=blue!60,
        halign=flush left, leftrule=4mm,
}}
\newtcolorbox{circbox}[2][]{common,
    hbox, title = {#2}, nobeforeafter, 
    #1}
\begin{document}
    \begin{circbox}{Polarización}
        \begin{tikzpicture}
            \draw[thick] (0,0) circle (1cm);
        \end{tikzpicture}
    \end{circbox}
    \begin{circbox}{Señal}
        \begin{tikzpicture}
            \draw[thick] (0,0) circle (1cm) (1.3,0) circle (1cm);
        \end{tikzpicture}
    \end{circbox}

    \bigskip

    \fbox{
        \begin{tikzpicture}
            \draw[thick] (0,0) circle (1cm);
        \end{tikzpicture}
    }
    \fbox{
        \begin{tikzpicture}
            \draw[thick] (0,0) circle (1cm) (1.3,0) circle (1cm);
        \end{tikzpicture}
    }
\end{document}

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.