4

I can't find a way to match the box width of a tikzpicture and the column width.

Here is the code :

\tikzstyle{mybox} = [draw=primary_shade3, fill=white, thick,
rectangle, rounded corners, inner ysep=10pt]
\tikzstyle{fancytitle} =[fill=white, text=primary_shade3]

\noindent \begin{tikzpicture}
\node [mybox] (box){%
\begin{minipage}{\linewidth}
\begin{itemize}
        \item[$\bullet$] De plus en plus de patients et de moins en moins de personnel
        \item[$\bullet$] Perte d'information lors du suivi à partir d'observations visuelles
        \item[$\bullet$] Contraintes de coût, de portabilité et d'utilisabilité des nouvelles \\
                         plate-formes
    \end{itemize}
\end{minipage} 
};
\node[fancytitle, right=10pt] at (box.north west) {Problématique};
\end{tikzpicture}

And the result : enter image description here

As you can see, the right of the box is outside the columnwidth... Do you know how to fix this ?

Best

  • 1
    please extend your code snippet to complete document (mwe: minimal working example) – Zarko Aug 2 '18 at 13:24
7

You should not use minipages in a tikz node. The problem was that the inner and outer seps were added which made the box overshoot. EDIT: I added a typeout that shows a bit what is done. (Of course one has to subtract these values twice, they are added left and right. And yes, tcolorbox as pointed out by Ignasi in his answer does all that for you, nevertheless I thought the question was why that happens. ;-)

\documentclass{article}
\usepackage{tikz,lipsum}
\begin{document}
\definecolor{primary_shade3}{rgb}{0.66,0.0,0.66} 
\tikzset{mybox/.style={draw=primary_shade3, fill=white, thick,
rectangle, rounded corners, inner ysep=10pt},
fancytitle/.style={fill=white, text=primary_shade3}}
\lipsum[1]

\noindent \begin{tikzpicture}
\pgfmathsetmacro{\mywidth}{\linewidth-2*\pgfkeysvalueof{/pgf/inner xsep}-2*\pgfkeysvalueof{/pgf/outer xsep}
-\pgflinewidth}
\typeout{the\space line\space width\space is\space\the\linewidth,\space and\space
the\space text\space width\space is\space\mywidth}
\node [mybox,text width=\linewidth-2*\pgfkeysvalueof{/pgf/inner xsep}-2*\pgfkeysvalueof{/pgf/outer xsep}
-\pgflinewidth] (box){%
\begin{itemize}
        \item[$\bullet$] De plus en plus de patients et de moins en moins de personnel
        \item[$\bullet$] Perte d'information lors du suivi à partir d'observations visuelles
        \item[$\bullet$] Contraintes de coût, de portabilité et d'utilisabilité des nouvelles \\
                         plate-formes
    \end{itemize}
};
\node[fancytitle, right=10pt] at (box.north west) {Problématique};
\end{tikzpicture}

\lipsum[2]
\end{document}

enter image description here

  • It works fine, thx but why do we have to add inner et outer sep of 2pt ? – Maxouille Aug 2 '18 at 13:36
  • 1
    @Maxouille You do not have to, this is just a simple trick to know what these dimensions are. You could also read out the predefined values with \pgfkeysgetvalue, do you want me to change the answer accordingly? – user121799 Aug 2 '18 at 13:40
  • If you can it would be great :) – Maxouille Aug 2 '18 at 13:53
  • @Maxouille Done. – user121799 Aug 2 '18 at 14:02
9

An easier solution would be to use tcolorbox. It's default size is always adjusted to \linewidth.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usetikzlibrary{babel}
\definecolor{primary_shade3}{rgb}{0.66,0.0,0.66} 

\newtcolorbox{mybox}[2][]{
    enhanced,
    colback=white,
    colframe=primary_shade3,
    coltitle=primary_shade3,
    fonttitle=\bfseries,
    attach boxed title to top left={xshift=3mm,yshift*=-\tcboxedtitleheight/2},
    boxed title style={
        colframe=white,
        colback=white,
        size=fbox
    },
    title=#2,
    #1
}

\begin{document}

\lipsum[1]

\begin{mybox}{Problématique}
\begin{itemize}
    \item[$\bullet$] De plus en plus de patients et de moins en moins de personnel
   \item[$\bullet$] Perte d'information lors du suivi à partir d'observations visuelles
   \item[$\bullet$] Contraintes de coût, de portabilité et d'utilisabilité des nouvelles plate-formes
    \end{itemize}
\end{mybox}

\lipsum[2]
\end{document}

enter image description here

  • nice answer (+1) ! i would add the package enumitem and than for itemize use: \begin{itemize}[label=\textbullet, leftmargin = *] \item De plus en plus ... \end{itemize} – Zarko Aug 2 '18 at 15:41

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.