# Automatic adjust font size in tikz to avoid overlapping

I have two nodes, one on the left that grows to the right with longer texts and the second is positioned on the right and grows to the left.

Now, if the node texts are too long, the nodes overlap. I want the left node to reduce its font size in that case. But the content is generted, therefore I need a solution where tikz does the font scaling, not me manually.

\documentclass{article}
\usepackage{tikz}

\newcommand{\card}[2]{
\begin{tikzpicture}
\node[anchor=south west] at (1mm,1mm) {#1};
\node[anchor=south east] at (40mm,1mm) {#2};
\end{tikzpicture}
}

\begin{document}
What I have:\\
\card{short text}{RIGHT}\\
\card{long long long text}{RIGHT}\\
What I want:\\
\card{short text}{RIGHT}\\
\card{\tiny long long long text}{RIGHT}\\
\end{document}


You can use \resizebox from graphicx.sty to scale the text if it is too long. I have assumed the total text (left and right) is 40mm, and that the right hand text should not be scaled. There is also a separation between the texts of 1mm. And I removed all the spacing in the nodes.

\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{calc}
\newsavebox\MyLeftBox
\newsavebox\MyRightBox
\newlength\MyLeftLength
\newcommand{\card}[2]{
\sbox\MyLeftBox{#1}
\sbox\MyRightBox{#2}
\setlength\MyLeftLength{40mm-\wd\MyRightBox-1mm}
\begin{tikzpicture}
%\draw[<->,gray!40](0,0)--(40mm,0);
\node[anchor=south west,inner sep=0pt,outer sep=0pt] at (0mm,1mm) {%
\ifdim \MyLeftLength < \wd\MyLeftBox
\resizebox{\MyLeftLength}{!}{#1}
\else
#1
\fi
};
\node[anchor=south east,inner sep=0pt,outer sep=0pt] at (40mm,1mm) {#2};
\end{tikzpicture}
}
%%%%%%
\begin{document}
\begin{tabular}{l}
\card{short text}{RIGHT}\\
\card{long long long long long text}{RIGHT}\\
\card{long long long long long text}{LONG RIGHT}
\end{tabular}
\end{document}


• works like a charm. I think both answers are similar, but somehow I prefere this one. – DerBaer Jun 17 '17 at 18:14

You might want to go with this. You need to preset the length \maximalCardLength.

Note that scaling a font like this is usually not that good. High quality fonts have individual designs for a couple of sizes, compare https://en.wikipedia.org/wiki/File:True_vs_Scaled_Small_Caps.svg.

\documentclass{article}
\usepackage{tikz}

\usepackage{ifthen}
\newsavebox{\cardbox}
\newlength{\saveboxLength}
\newlength{\maximalCardLength}\setlength{\maximalCardLength}{22mm}
\newcommand{\card}[2]{
\savebox{\cardbox}{#1}
\settowidth{\saveboxLength}{\usebox{\cardbox}}

\ifdim\saveboxLength<\maximalCardLength
\setlength{\saveboxLength}{\maximalCardLength}
\fi

\begin{tikzpicture}
\node[anchor=south west, scale=\maximalCardLength/\saveboxLength] at (1mm,1mm) {\usebox{\cardbox}};
\node[anchor=south east] at (40mm,1mm) {#2};
\end{tikzpicture}
}

\begin{document}

What I have:\\
\card{short text}{RIGHT}\\
\card{long long long text}{RIGHT}\\
What I want:\\
\card{short text}{RIGHT}\\
\card{\tiny long long long text}{RIGHT}\\
\end{document}