3

Question

I would like to put a minipage environment inside of a tikz node. I know this is done already with the align=left command, but if you set a minimum width of the node to \linewidth, then the text is left aligned, but surrounded by white space padding (depending on how much text you include)

Problem

When I set the width of the minipage environment to \linewidth, minipage grabs the width of the line on a page instead of the width of the node.

Sample Code

\newcommand{\warning}[1]{%
% Define box and box title style
\tikzstyle{mybox} = [align=left,draw=red, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=10pt,maximum width=\linewidth]
\tikzstyle{fancytitle} =[fill=red, text=white,rounded corners]
\begin{tikzpicture}
    \node [mybox] (box){%
        \begin{minipage}{1\textwidth}
        #1
        \end{minipage}
        };
    \node[fancytitle, right=10pt] at (box.north west) {Note:};
\end{tikzpicture}
}%

Adapted code from http://www.texample.net/tikz/examples/boxes-with-text-and-math/

2
  • the node does not have a width initially (it is an hbox that is just set the natural width of the content. If you set text width = ... it has a natural width (and it's already a minipage Dec 5, 2014 at 9:18
  • 1
    Actually, its a \parbox, not a minipage. It does not like having \par inside! ;-) Dec 5, 2014 at 20:52

1 Answer 1

6

Like this?

 \documentclass{article}
\usepackage{tikz,lipsum,showframe}
\tikzset{mybox/.style = {text width=\dimexpr\linewidth-20pt-\pgflinewidth\relax,align=justify,draw=red,
                very thick, rectangle, rounded corners, inner sep=10pt, inner ysep=10pt},
        fancytitle/.style ={fill=red, text=white,rounded corners}
}
\begin{document}
\noindent
\begin{tikzpicture}
    \node [mybox] (box){%
        \lipsum[1]
        };
    \node[fancytitle, right=10pt] at (box.north west) {Note:};
\end{tikzpicture}
\end{document}

enter image description here

With an explicit minipage as requested:

\documentclass{article}
\usepackage{tikz,lipsum,showframe}
\tikzset{mybox/.style = {align=center,draw=red,
                very thick, rectangle, rounded corners, inner sep=10pt, inner ysep=10pt},
        fancytitle/.style ={fill=red, text=white,rounded corners}
}
\begin{document}
\noindent
\begin{tikzpicture}
    \node [mybox] (box){%
        \begin{minipage}{\dimexpr\linewidth-20pt-\pgflinewidth\relax}
          \lipsum[1]
        \end{minipage}%
        };
    \node[fancytitle, right=10pt] at (box.north west) {Note:};
\end{tikzpicture}
\end{document}
12
  • +1: I would suggest to not reinvent the wheel: mdframed and tcolorbox does this job better :) Dec 5, 2014 at 9:22
  • @ClaudioFiandrino: I was about to write: But why don't you use tcolorbox? and then you commented! :) I will add a tcolorbox version in some time.
    – user11232
    Dec 5, 2014 at 9:24
  • @ Harish: :-) Dec 5, 2014 at 9:26
  • No, not like that. I asked for a minipage inside of TikZ node. Dec 5, 2014 at 9:28
  • 1
    @macmadness86 Yes, 10pt on left and 10pt on right so 20 pt. And \pgflinewidth is half for left and half for right side.
    – user11232
    Dec 5, 2014 at 10:18

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.