1

Is it possible to calculate the length of a text written by TikZ?

I would like to fit a word inside circle. But sometimes the word is to long for the circle. So my idea was to calculate the length of the text and to adjust the font size if the text is too long.

Pascal
  • 779
  • 3
    width("some text here"). Sort of related: https://tex.stackexchange.com/questions/373941/flow-chart-with-defined-width-for-the-boxes/374271#374271 – Torbjørn T. Aug 04 '17 at 11:58
  • Also see https://tex.stackexchange.com/questions/284314/specify-width-height-of-rectangle-around-text-using-tikz and https://tex.stackexchange.com/questions/225533/balanced-text-wrapping-in-parbox. – John Kormylo Aug 04 '17 at 14:38

1 Answers1

4

May be a round fitbox (from tcolorbox) can help you. You just define box size and tcolorbox fits the text inside it. Only texts larger than provided space are reduced.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lmodern}

\newtcboxfit{\myfitbox}{%
    width = 3cm,
    square,
    circular arc,
    halign = center,
    valign = center,
    nobeforeafter
}


\begin{document}
\myfitbox{This is a long text inside a circular tcolorbox}
%
\myfitbox{This is an ever longer text inside a circular tcolorbox}
%
\myfitbox{This is a much more longer, longer and  longer text inside a circular tcolorbox}
%
\myfitbox{This is a much more longer, longer and  longer text inside a circular tcolorbox. This is a much more longer, longer and  longer text inside a circular tcolorbox}
\end{document}

enter image description here

Ignasi
  • 136,588