Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to shrink the text in a node such that the minimum height and width are the actual width and height of the node.

I found this Shrinking text to the width of a node within a tikzpicture, but I don't want to first generate the node n in that example.

I need something like:

\begin{tikzpicture} 

    \tikzstyle{D}=[shape=diamond, draw, line width=0.2pt, minimum width=0.8cm,   minimum height=0.8cm,inner sep = 0pt]

      \node [D] (m) {\shrinktofitinnode{AAAAAAAA}{??}...{??};
\end{tikzpicture}

If someone could help me, that would be really good.

Does anyone know a way to calculate the maximal text box that fits into a shape of a certain size?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

You can get the value of the minimum width key using \pgfkeysvalueof{/pgf/minimum width} etc. and use it with \resizebox (graphicx package, already loaded by tikz} to resize the content to it. For the diamond shape you might want to reduce the width a little more (e.g. to 90%; for tall texts even down to 50%), otherwise the text will touch the lines.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}

\begin{tikzpicture}[D/.style={shape=diamond, draw, line width=0.2pt, minimum width=0.8cm, minimum height=0.8cm,inner sep=0pt}]

 \node [D] (m) {\setkeys{Gin}{keepaspectratio}\resizebox{\pgfkeysvalueof{/pgf/minimum width}}{\pgfkeysvalueof{/pgf/minimum width}}{AAAAAAAA}};

 \node [D] (k) at (2,0) {\setkeys{Gin}{keepaspectratio}\resizebox{.9\dimexpr\pgfkeysvalueof{/pgf/minimum width}\relax}{.9\dimexpr\pgfkeysvalueof{/pgf/minimum height}\relax}{AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA}};

\end{tikzpicture}

\end{document}

Result


You can also use adjustbox for easy further adjustments, e.g. to allow line breaks. The similar TikZ options won't work as normal because \resizebox is used.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\usepackage{adjustbox}
\begin{document}

\begin{tikzpicture}[D/.style={shape=diamond, draw, line width=0.2pt, minimum width=0.8cm, minimum height=0.8cm,inner sep=0pt}]

 \node [D] (m) {\setkeys{Gin}{keepaspectratio}\resizebox{\pgfkeysvalueof{/pgf/minimum width}}{\pgfkeysvalueof{/pgf/minimum width}}{AAAAAAAA}};

 \node [D] (k) at (2,0) {\adjustbox{varwidth={5cm}\centering,keepaspectratio,width=.9\dimexpr\pgfkeysvalueof{/pgf/minimum width}\relax,height=.9\dimexpr\pgfkeysvalueof{/pgf/minimum height}\relax}{AAAAAAAAAAAAAA\\BBBBBBBBBBBB\\CCCCCCCCC}};

\end{tikzpicture}

\end{document}

Result


Finally there is the min size key of \adjustbox which will scale the content down only if required (i.e. it is too large), but keep it the original size otherwise. You might want to define a shortcut macro if you need this more often.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\usepackage{adjustbox}

\newcommand{\adjsizetonode}{%
  \adjustbox{varwidth={5cm}\centering,keepaspectratio,max size={.9\dimexpr\pgfkeysvalueof{/pgf/minimum width}\relax}{.9\dimexpr\pgfkeysvalueof{/pgf/minimum height}\relax}}%
}
\begin{document}

\begin{tikzpicture}[D/.style={shape=diamond, draw, line width=0.2pt, minimum width=0.8cm, minimum height=0.8cm,inner sep=0pt}]

 \node [D] (A)  at (0,0) {a};
 \node [D] (A2) at (1,0) {\adjsizetonode{a}};
 \node [D] (B)  at (2,0) {\adjsizetonode{BBBBBBBBBBBBBBBBBB}};
 \node [D] (B)  at (3,0) {\adjsizetonode{BBBBBBBBBBBBBBBBBB\\bbbbbbbbbbbbb bbbbbbbb bbbb bbbb\\Bb bbfd  kslkgldf }};

\end{tikzpicture}

\end{document}

Result

share|improve this answer
    
Thank you very much. I tried the third code and it worked really good except ehen I changed the font size with for example \fontsize{30}{36}\selectfont. Do you have any idea how i can use an arbitrary font size and still get the shapes so nicely adjusted? –  Jana Apr 19 '12 at 9:41
    
@Jana: A changed font size should not be an issue. Put the font commands either inside the node or use the font=<..> TikZ option in the D/.style to set the font. AFAIK, TikZ resets the font at the begin of every node, so external font changes won't have an effect. –  Martin Scharrer Apr 19 '12 at 9:50
    
Thank you, I didn't know that. One more question: I want to have a little space between the lines of the shape and the text. How do I do that? With the option inner sep = 1pt, the size is getting larger. Thank you so much for your help, I am new to tikz and just starting to find my way around. –  Jana Apr 19 '12 at 10:04
    
@Jana: The minimum width and minimum height is for the shape (i.e. the border lines) not the text. So you have to scale to this length minus the space you want. I mentioned that in my answer, i.e. only use .9 (90%) of the width and height. You can either reduce .9 to a lower value, e.g. .8 or subtract a constant value, e.g. max size={\pgfkeysvalueof{/pgf/minimum width}-1pt}{\pgfkeysvalueof{/pgf/minimum height}-1pt} should work. –  Martin Scharrer Apr 19 '12 at 10:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.