[Tex/LaTex] Fitting text to exact specific size

boxesfontsizeformatting

I would like to make my text fit a certain size "box" at a specific location. The two problems I am encountering are as follows:

This long string should wrap when it hits the 4.5 inch limit but it does not:

\documentclass[landscape]{article}
\usepackage[top=1.5in, bottom=1.125in, left=.25in, right=6.25in,textwidth=4.5in, textheight=5.875in]{geometry}

\begin{document}
ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
\end{document}

And this one just remains constant but I would like to force it to fit the entire 4.5in x 5.875in box, as in enlarge the text to make it fit the box.

\documentclass[landscape]{article}
\usepackage[top=1.5in, bottom=1.125in, left=.25in, right=6.25in,textwidth=4.5in, textheight=5.875in]{geometry}

\begin{document}
{\Huge This should fit in a 4.5in x 5.875in box but that is close to the left edge of paper, but it is not conforming to the box size desired....
\end{document}

Thanks for the help.

Best Answer

This could be a job for the fitbox options of package tcolorbox. As commented, your first text needs hyphenations. But your second text can sized using the following code. I added a yellow background to show the 4.5in x 5.875in box.

Note that I included a scalable font package (lmodern) to enable the font scaling mechanism.

\documentclass[landscape]{article}
\usepackage[top=1.5in, bottom=1.125in, left=.25in, right=6.25in,textwidth=4.5in, textheight=5.875in]{geometry}
\usepackage{lmodern}% for scalable fonts!
\usepackage[many]{tcolorbox}

\begin{document}
\tcboxfit[blank,width=4.5in,height=5.875in,
  fit basedim=20pt,fit fontsize macros,valign=center,
  frame style={fill=yellow!50!white}% remove this line to remove the yellow background
]%
{\Huge This should fit in a 4.5in x 5.875in box but that is close to the left edge of paper, but it is not conforming to the box size desired....}
\end{document}

enter image description here

The first code adapts the font to fit the text inside the given box. Still, at the top and bottom of the box remain two very small blank areas. If it is important to remove them, a final stretching can be added using the following code. Note that the outer box is just for coloring the example.

\documentclass[landscape]{article}
\usepackage[top=1.5in, bottom=1.125in, left=.25in, right=6.25in,textwidth=4.5in, textheight=5.875in]{geometry}
\usepackage{lmodern}% for scalable fonts!
\usepackage[many]{tcolorbox}

\begin{document}
\tcbox[blank,width=4.5in,height=5.875in,frame style={fill=yellow!50!white}]{% just for the background
%
\resizebox{4.5in}{5.875in}{\tcboxfit[blank,width=4.5in,fit height from=5in to 5.875in,fit basedim=20pt,fit fontsize macros]%
{\Huge This should fit in a 4.5in x 5.875in box but that is close to the left edge of paper, but it is not conforming to the box size desired....}}
%
}% just for the background
\end{document}

enter image description here

Related Question