# How to make a box with fixed size?

\parbox[pos][height][contentpos]{width}{text} gives me a box where height is actually "minimal height", while width= exact width. I would like height to also be "exact height". How can I do this? Text that is larger then the content should

1. disappear "within the box" and not appear outside the boxes borders or
2. be adjusted in size so that it fits into the box.

Though I think 2 is asking to much.

Here is how far I got:

\RequirePackage[l2tabu, orthodox]{nag}      % Warning when old or wrong commands are used
\RequirePackage{fix-cm}             % permits Computer Modern fonts at arbitrary sizes
\documentclass[a4paper,10pt,oneside]{article}

\usepackage{geometry}
\usepackage{blindtext}
\usepackage{adjustbox}

\usepackage[many]{tcolorbox}
\usepackage{lmodern}% or any other vector / postscript font
\usepackage{lipsum}

\geometry{
a4paper,
centering,
hmargin=0.5mm,
vmargin=1mm,
ignoreall,
showframe
}

\setlength{\parindent}{0cm}
\setlength{\fboxsep}{0pt}

\begin{document}
\selectlanguage{ngerman}
\renewcommand{\familydefault}{\sfdefault}
\pagestyle{empty}

\newcommand{\myboxI}{%
\fbox{\adjustbox{totalheight=21.2mm, width=52.5mm, keepaspectratio, rotate=0, minipage=[r][21.2mm][t]{52.5mm}}{%
wefewf wwkeföjwlkej fkewjf ölkewjflkewjf \\ ölkewjf lwkejf lkwjlkwejf lkwejf \\ asdf wef we \\ werwer we \\ werwerewfewfewfewfewfwef wef ewf ewf wef we f \\ wefewf wefwe%
}}%
}

\newcommand{\myboxII}{%
\begin{tcolorbox}[%
fit,width=52.5mm,height=21.2mm,blank,
borderline={0.4pt}{0pt}{red!20!white},
%watermark text={6cm $\times$ 6cm},nobeforeafter
]
wefewf wwkeföjwlkej fkewjf ölkewjflkewjf \\ ölkewjf lwkejf lkwjlkwejf lkwejf \\ asdf wef we \\ werwer we \\ werwerewfewfewfewfewfwef wef ewf ewf wef we f \\ wefewf wefwe
\end{tcolorbox}%
}

\myboxI\hspace{-1cm}\hfill\myboxI\hspace{-1cm}\hfill\myboxI\hspace{-1cm}\hfill\myboxI

\bigskip

\myboxII\hspace{-1cm}\hfill\myboxII\hspace{-1cm}\hfill\myboxII\hspace{-1cm}\hfill\myboxII

\end{document}


The first attempt does not really scale the content, as I had hoped. Also I had expected that the boxes are just next to each other, not overlap.

The second does not allow the boxes to be next to each other.

• For problem 2, see tex.stackexchange.com/questions/144299/… – egreg Apr 15 '16 at 20:53
• you can enforce a "uniform" height for the normal text size by placing a \strut at the beginning of the text in the box, and another at the end of that text (no intervening space) to mimic the normal baselines and ensure the depth of descenders in the bottom line. – barbara beeton Apr 15 '16 at 20:55
• Also for problem 2, tex.stackexchange.com/questions/123614/… – Steven B. Segletes Apr 15 '16 at 22:16
• There is no point in loading fix-cm if you load lmodern that I know of. – cfr Apr 15 '16 at 22:44

## 2 Answers

Problem 2 is treated elsewhere on the site, I'll deal only with problem 1, which can be solved with \vsplit.

\documentclass{article}
\usepackage{xparse}
\usepackage{microtype}
\usepackage[margin=1cm]{geometry}

\usepackage{lipsum}

\ExplSyntaxOn
\box_new:N \l_hideparbox_box

\NewDocumentCommand{\hideparbox}{O{c}mm+m}
{% #1=alignment, #2=height, #3=width, #4=text
\group_begin:
\vbox_set:Nn \l_hideparbox_box
{
\use:c { @parboxrestore }
\hsize=#3\scan_stop:
\strut#4\par
}
\vbadness=\c_ten_thousand % no spurious underfull messages
\vbox_set_split_to_ht:NNn \l_hideparbox_box \l_hideparbox_box { #2 }
\parbox[#1][#2]{#3}
{
\vbox_unpack:N \l_hideparbox_box
}
\group_end:
}
\ExplSyntaxOff

\begin{document}

\fboxsep=0pt % just for the example

\fbox{\hideparbox{3cm}{7cm}{\lipsum*[1]}}
\fbox{\hideparbox{6cm}{7cm}{\lipsum*[1]}}

\fbox{\hideparbox[t]{3cm}{7cm}{\lipsum*[1]}}
\fbox{\hideparbox[t]{6cm}{7cm}{\lipsum*[1]}}

\fbox{\hideparbox[b]{3cm}{7cm}{\lipsum*[1]}}
\fbox{\hideparbox[b]{6cm}{7cm}{\lipsum*[1]}}

\end{document}


I load microtype just for convenience, in order to minimize overfull boxes. Also \fbox is used just for showing the boundaries of the boxes.

The idea is to typeset the box and then prune off everything beyond the stated limit.

• 1) How do I get rid of the white space between the boxes? 2) I tried to solve problem 2 - as shown in my edited question - but not with enough success, yet... 3) How can make a white margin inside of the boxes? – Make42 Apr 15 '16 at 21:23
• @Make42 That's the end of line; I left it for better separating the boxes. Since the two problems are independent, it's better having two separate questions. – egreg Apr 15 '16 at 21:26
• I forgot that the end-of-line creates whitespace - now it works. But why does this not go for my solution 2 attempt from my question? (Also, can you help 2) and 3)? Or where you referring to 3) being a separate question?) – Make42 Apr 15 '16 at 21:31
• The same problem with the white horizontally, I have vertically as well. – Make42 Apr 15 '16 at 21:50
• @Make42 I already suggested to bring problem 2 into another question. – egreg Apr 15 '16 at 21:59

My solutions answers both requirements.

The implementation version A creates a box which hides text which does not fit into the box.

The implementation version B creates a box where the text is adjusted to the box dimensions.

For both cases, I used a inner white margin of 1mm.

\RequirePackage[l2tabu, orthodox]{nag}      % Warning when old or wrong commands are used
\RequirePackage{fix-cm}             % permits Computer Modern fonts at arbitrary sizes
\documentclass[a4paper,10pt,oneside]{article}

\usepackage[ngerman]{babel}
\usepackage{geometry}

\usepackage[many]{tcolorbox}
\usepackage{lmodern}% or any other vector / postscript font
\usepackage{lipsum}

\geometry{
a4paper,
centering,
hmargin=0.0mm,
vmargin=1mm,
ignoreall,
showframe
}

\setlength{\parindent}{0cm}
\setlength{\fboxsep}{0pt}

\begin{document}
\selectlanguage{ngerman}
\renewcommand{\familydefault}{\sfdefault}
\pagestyle{empty}

\textbf{Version A: Text disappears:}

\newtcbox{\myboxA}{blank,boxsep=1mm,
clip upper,minipage,
width=52.5mm,height=21.2mm,nobeforeafter,
borderline={0.4pt}{0pt}{blue!20!white},
}

\myboxA{%
wefewf wwkeföjwlkej fkewjf ölkewjflkewjf \\ ölkewjf lwkejf lkwjlkwejf lkwejf \\ asdf wef we \\ werwer we \\ werwerewfewfewfewfewfwef wef ewf ewf wef we f \\ wefewf wefwe%
}%
\myboxA{%
wefewf wwkeföjwlkej fkewjf ölkewjflkewjf \\ ölkewjf lwkejf lkwjlkwejf lkwejf \\ asdf wef we \\ werwer we \\ werwerewfewfewfewfewfwef wef ewf ewf wef we f \\ wefewf wefwe%
}%
\myboxA{%
wefewf wwkeföjwlkej fkewjf ölkewjflkewjf \\ ölkewjf lwkejf lkwjlkwejf lkwejf \\ asdf wef we \\ werwer we \\ werwerewfewfewfewfewfwef wef ewf ewf wef we f \\ wefewf wefwe%
}%
\myboxA{%
wefewf wwkeföjwlkej fkewjf ölkewjflkewjf \\ ölkewjf lwkejf lkwjlkwejf lkwejf \\ asdf wef we \\ werwer we \\ werwerewfewfewfewfewfwef wef ewf ewf wef we f \\ wefewf wefwe%
}%

\bigskip
\textbf{Version B: Text adjusted in size:}

\newtcbox{\myboxB}{blank,boxsep=1mm,
clip upper,fit,fit algorithm=hybrid*,
width=52.5mm,height=21.2mm,nobeforeafter,
borderline={0.4pt}{0pt}{red!20!white},
}

\myboxB{%
wefewf wwkeföjwlkej fkewjf ölkewjflkewjf \\ ölkewjf lwkejf lkwjlkwejf lkwejf \\ asdf wef we \\ werwer we \\ werwerewfewfewfewfewfwef wef ewf ewf wef we f \\ wefewf wefwe%
}%
\myboxB{%
wefewf wwkeföjwlkej fkewjf ölkewjflkewjf \\ ölkewjf lwkejf lkwjlkwejf lkwejf \\ asdf wef we \\ werwer we \\ werwerewfewfewfewfewfwef wef ewf ewf wef we f \\ wefewf wefwe%
}%
\myboxB{%
wefewf wwkeföjwlkej fkewjf ölkewjflkewjf \\ ölkewjf lwkejf lkwjlkwejf lkwejf \\ asdf wef we \\ werwer we \\ werwerewfewfewfewfewfwef wef ewf ewf wef we f \\ wefewf wefwe%
}%
\myboxB{%
wefewf wwkeföjwlkej fkewjf ölkewjflkewjf \\ ölkewjf lwkejf lkwjlkwejf lkwejf \\ asdf wef we \\ werwer we \\ werwerewfewfewfewfewfwef wef ewf ewf wef we f \\ wefewf wefwe%
}%

\end{document}


• Guess I should really learn tcolorbox... – Make42 Apr 18 '16 at 6:53