6

\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}

enter image description here

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.

Make42
  • 1,772
  • For problem 2, see http://tex.stackexchange.com/questions/144299/insert-text-into-a-fixed-size-box – 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, http://tex.stackexchange.com/questions/123614/making-the-text-fit-in-a-specific-space-in-latex – 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 Answers2

4

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.

enter image description here

egreg
  • 1,121,712
  • 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
  • (Sad to see, nobody had upvoted yet…) In the \vbox_set:Nn command: \unskip\strut\par after #4 as well? – GuM Apr 15 '16 at 22:45
  • @GustavoMezzetti That's not necessary: it's not known where the break will happen. Maybe a correction after the \unvbox, but I don't think it's necessary. – egreg Apr 15 '16 at 22:47
  • Yes, you are right: it is not only not necessary, it is wrong! – GuM Apr 15 '16 at 22:50
  • What is this syntax btw? How did you come up with that? Haven't seen anything like it so far. – Make42 Apr 15 '16 at 23:33
  • @Make42 It's expl3. Not really necessary here, but handy when optional arguments are involved – egreg Apr 16 '16 at 08:40