# dynamically adjust font size based on length of input name

I have a document where title page includes the name of client. Font size for the name is fixed and for short names it works fine. However, whenever I have a client with longer name, my text goes over the document margins.

Is there any way to automatically adjust the font size for the name, based on the input? Text is being placed in tikz picture with some background.

Here is what I got so far, MWE, which I tried based on answers I have found previously.

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

\newsavebox\MyNameBox
\newsavebox\MySurnameBox
\newlength\MyNameLength

\newcommand{\nametag}[2]{
\sbox\MyNameBox{#1}
\sbox\MySurnameBox{#1~#2}
\setlength\MyNameLength{40mm-1mm}
\edef\boxtrue{0}
\begin{tikzpicture}
\node[anchor=south east,inner sep=0pt,outer sep=0pt,align=left] at (0mm,0mm) {
\ifdim \MyNameLength < \wd\MySurnameBox
%    \ifdim \MyNameLength < \wd\MySurnameBox
\edef\boxtrue{1}
\resizebox{\MyNameLength}{!}{#1~#2}
\else
#1~#2
\fi
};

\end{tikzpicture}
}
%%%%%%
\begin{document}

\nametag{FirstName}{Reallylooooooooooonglastname}\\

\end{document}


This works if I want to write first name and last name in one line, but I want to break them in 2 lines: First Name and below Last name, with same font size.

What happens if I break the names (first and last name) apart, I only get to resize the bigger one, while the other one has larger font, like below.

Code this part is like this.

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

\newsavebox\MyNameBox
\newsavebox\MySurnameBox
\newlength\MyNameLength

\newcommand{\nametag}[2]{
\sbox\MyNameBox{#1}
\sbox\MySurnameBox{#2}
\setlength\MyNameLength{40mm-1mm}
\edef\boxtrue{0}
\begin{tikzpicture}
\node[anchor=south east,inner sep=0pt,outer sep=0pt,align=left] at (0mm,-5mm) {
\ifdim \MyNameLength < \wd\MySurnameBox
%    \ifdim \MyNameLength < \wd\MySurnameBox
\edef\boxtrue{1}
\resizebox{\MyNameLength}{!}{#2}
\else
#2
\fi
};
\node[anchor=south east,inner sep=0pt,outer sep=0pt] at (0mm,0mm) {
\ifdim \MyNameLength < \wd\MyNameBox
\resizebox{\MyNameLength}{!}{#1}
\else
#1
\fi
};
\end{tikzpicture}
}
%%%%%%
\begin{document}

\nametag{FirstName}{Reallylooooooooooooooooooonglastname}\\

\end{document}


Any help would be really appreciated.. Thanks..

• The code you provide yields the first screen shot. What is the code for the second, unwanted screen shot? – user121799 Jul 2 at 23:22
• I added the second part.. – Almic7 Jul 3 at 6:21