1
\documentclass{minimal}  
\usepackage{tikz}
\usepackage{lipsum}

\begin{document}

\noindent 
\begin{tikzpicture}[trim left = (box.west)] 
\node[draw = red] (box) {%
    \begin{minipage}{\textwidth} 
      \lipsum[1]
    \end{minipage}
    };
\node[left = 1cm] at (box.west) {hello};
\end{tikzpicture} 
      
\lipsum[2]
\end{document}

Is there a way to force the box to have the same width as the text above ?

Without using if possible

    \begin{minipage}{0.94\textwidth} 

Thanks

enter image description here

3
  • 1
    Your question is not quite clear to me, but since you asked for a minipage with a width of textwidth, your box is obviously larger than the text block below. Could you elaborate about what your problem is exactly?
    – SebGlav
    Aug 19, 2022 at 14:38
  • 2
    A node includes the inner xsep and the inner ysep value. The red border adds by default an outer xsep and outer ysep to the size of the node (by default half the line width) so that the anchors of the node lie at the actual outside. So your actual TikZpicture is \textwidth + 2*/pgf/inner xsep + 2*/pgf/outer xsep wide. (But remember the bounding box issue of your previous question which would have ignored any outer seps anyway.) So: \begin{minipage}{\dimexpr\textwidth-.6666ex-.4pt\relax} would be a start. (Default inner sep is .3333ex and default linewidth is .4pt.) Aug 19, 2022 at 14:49
  • Maybe have a look at the tcolorbox package? Aug 19, 2022 at 14:52

2 Answers 2

2

Try the following:

\documentclass{article}
\usepackage{microtype}
\usepackage{tikz}
\usepackage{lipsum}

\begin{document}

\noindent
\begin{tikzpicture}[trim left = (box.west)] 
\node[draw = red, align=justify,
      text width =\textwidth-2*\pgfkeysvalueof{/pgf/inner xsep}] (box) {\lipsum[1]};
\node[left = 1cm] at (box.west) {hello};
\end{tikzpicture}

\lipsum[2]
\end{document}

As you can see, minipage is removed, but defined is node's text width.

enter image description here

Is this what you after?

3
  • What is trim left = (box.west)? How does it work?
    – Sigur
    Aug 19, 2022 at 15:14
  • 1
    @Sigur, i just copied from OP question :-), however it is described in 15.8 Establishing a Bounding Box (pp184) on page 185 of Ti*kKZ \& PGF manual (version 3.1.9a).
    – Zarko
    Aug 19, 2022 at 17:41
  • 2
    @Sigur See that post where everything started about trim.
    – SebGlav
    Aug 19, 2022 at 17:42
1

You can use a tcolorbox. It's default width is \linewidth.

\documentclass{article}
\usepackage{microtype}
\usepackage{tikz}
\usepackage{lipsum}

\begin{document}

\noindent
\begin{tikzpicture}[trim left = (box.west)] 
\node[draw = red, align=justify,
      text width =\textwidth-2*\pgfkeysvalueof{/pgf/inner xsep}] (box) {\lipsum[1]};
\node[left = 1cm] at (box.west) {hello};
\end{tikzpicture}

\lipsum[2]
\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 .