7

How to alter the following code, to have the tikzpicture to have the exact width and height of the minipage containing it (without changing the literal coordinates of course :))?

\begin{minipage}{2cm}

\begin{tikzpicture}

\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);

\end{tikzpicture}

\end{minipage}
Sigur
  • 37,330
MCH
  • 377

1 Answers1

7

While the \resizebox will scale the picture, it will also scale the text in it. This is usually not desired.

Below, I defined \MyResizeBox which measures the width of the picture and then produces the images with an appropriate scale, so that only the graphics are scaled, yet the text remains the same size

enter image description here

Notes:

  • I added a \fbox so that we could see the minipage boundary.
  • The tabluar is only so that I could obtain a nice image to post here -- It is not related to the the solution.
  • The tikzpicture was packaged into a macro to eliminate repeating the code.

Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}

\newcommand{\MyScale}{1}% \newcommand{\MyPicture}{% \begin{tikzpicture}[scale=\MyScale] \draw [ultra thick, blue] (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0); \node at (2,2) {Some text}; \end{tikzpicture}% }%

\newcommand*{\MyResizeBox}[2]{% \sbox0{#2}% \pgfmathsetmacro{\MyScale}{#1/\wd0}% #2% }%

\begin{document}

\begin{tabular}{p{5.0cm} p{4.0cm} p{4.0cm}} Original & \verb|\resizebox| & \verb|\MyResizeBox| \

\fbox{% \begin{minipage}{3cm} \MyPicture% \end{minipage}% }% & \fbox{% \begin{minipage}{3cm} \resizebox{3.0cm}{3.0cm}{\MyPicture}% \end{minipage}% }% & \fbox{% \begin{minipage}{3cm} \MyResizeBox{3.0cm}{\MyPicture}% \end{minipage}% } \% \end{tabular} \end{document}

Peter Grill
  • 223,288