如何制作一个固定尺寸的盒子?

如何制作一个固定尺寸的盒子?

\parbox[pos][height][contentpos]{width}{text}给我一个框,其中height实际上是“最小高度”,而width= 精确宽度。我希望高度也是“精确高度”。我该怎么做?文本大于内容应该

  1. 消失在“框内”,不会出现在框边界之外,或者
  2. 调整尺寸以便适合放入盒子中。

但我认为 2 要求太多了。

以下是我取得的进展:

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

在此处输入图片描述

第一次尝试并没有像我希望的那样真正缩放内容。我还以为这些框只是彼此相邻,而不是重叠。

第二个不允许盒子彼此相邻。

答案1

问题 2 已在网站的其他地方处理,我只处理问题 1,该问题可通过 解决\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}

我加载microtype只是为了方便,以尽量减少过满的框。也仅\fbox用于显示框的边界。

这个想法是排版框,然后修剪超出规定限制的所有内容。

在此处输入图片描述

答案2

我的解决方案满足了这两个要求。

实现版本 A 创建一个框,该框隐藏了不适合该框的文本。

实现版本 B 创建一个框,其中文本根据框尺寸进行调整。

对于这两种情况,我都使用了 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}

在此处输入图片描述

相关内容