我如何制作延伸至 fbox 边缘的垂直规则?

我如何制作延伸至 fbox 边缘的垂直规则?

考虑以下 MWE:

\documentclass{minimal}

\begin{document}
\fbox{some text \vrule{} more text}
\fbox{some text \vrule{} more deep text}
\fbox{some text \vrule{} more $\int$ tall text}
\end{document}

在这三种情况下,都会延伸到周围框的高度,但由于而\vrule不会接触到的边缘。\fbox\fboxsep

我怎样才能多做\vrule一点\fboxsep在两边都 我发现这个问题,但它只涉及为提供明确的高度\vrule,而我想将其延长一个给定的量,但除此之外让它根据需要扩大和缩小以填充当前框。

答案1

您可以使用\smash{\vrule},但必须指定高度和深度。

\documentclass{article}
\begin{document}
\sbox0{\fbox{some text more text}}%
\fbox{some text \smash{\vrule height\ht0 depth\dp0} more text}
\sbox0{\fbox{some text more deep text}}%
\fbox{some text \smash{\vrule height\ht0 depth\dp0} more deep text}
\sbox0{\fbox{some text more $\int$ tall text}}%
\fbox{some text \smash{\vrule height\ht0 depth\dp0} more $\int$ tall text}
\end{document}

演示


或者您可以只使用\struts 并使所有\fboxes 具有相同的高度。

\documentclass{standalone}
\begin{document}
\fbox{\strut some text}\hspace{-\fboxrule}\fbox{\strut some text}
\fbox{\strut some text}\hspace{-\fboxrule}\fbox{\strut more deep text}
\fbox{\strut some text}\hspace{-\fboxrule}\fbox{\strut more $\int$ tall text}
\end{document}

演示2

答案2

一种方法是设置\fboxsep为零,然后\vrule使用相应的设置\rule

\documentclass{minimal}
\setlength\fboxsep{0pt}
\newcommand\VR{\rule[-0.4\baselineskip]{0.4pt}{1.2\baselineskip} }

\begin{document}
\fbox{ some text \VR qjf more text }
\fbox{ some text \VR more deep text }
\fbox{ some text \VR more $\int$ tall text }
\end{document}

在此处输入图片描述

答案3

您可以测量文本的自然高度和深度,添加当前文本\fboxsep,并在两侧使用合适的支撑和字距重新排版,并将其设置\fboxsep为零。

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\vfbox}{m}
 {
  \clement_vfbox:n { #1 }
 }

\box_new:N \l__clement_vfbox_box
\dim_new:N \l__clement_vfbox_ht_dim
\dim_new:N \l__clement_vfbox_dp_dim
\dim_new:N \l__clement_vfbox_sep_dim

\AtBeginDocument
 {
  \dim_set:Nn \l__clement_vfbox_sep_dim { \fboxsep }
 }

\cs_new:Nn \clement_vfbox_strut:nn
 {
  \vrule height #1 depth #2 width 0pt
 }

\cs_new_protected:Nn \clement_vfbox:n
 {
  \group_begin:
  \dim_set:Nn \fboxsep { \l__clement_vfbox_sep_dim }
  \hbox_set:Nn \l__clement_vfbox_box { #1 }
  \dim_set:Nn \l__clement_vfbox_ht_dim { \box_ht:N \l__clement_vfbox_box + \fboxsep }
  \dim_set:Nn \l__clement_vfbox_dp_dim { \box_dp:N \l__clement_vfbox_box + \fboxsep }
  \dim_set:Nn \fboxsep { 0pt }
  \fbox
   {
    \clement_vfbox_strut:nn { \l__clement_vfbox_ht_dim } { \l__clement_vfbox_dp_dim }
    \kern \l__clement_vfbox_sep_dim
    #1
    \kern \l__clement_vfbox_sep_dim
   }
  \group_end:
 }
\ExplSyntaxOff

\begin{document}

\vfbox{some text \vrule{} more text}
\vfbox{some text \vrule{} more deep text}

\bigskip

\vfbox{some text \vrule{} more $\int$ tall text}
\vfbox{some text \vrule{} more $\displaystyle\int$ tall text}

\bigskip

\vfbox{some text \vrule{} \vfbox{some text \vrule{} other} other}

\end{document}

在此处输入图片描述

答案4

在我看来像是一个tabular

\documentclass{article} % see https://tex.stackexchange.com/q/42114/121799
\begin{document}
\begin{tabular}{|l|r|}
\hline
some text & more text\\
\hline
\end{tabular}

\begin{tabular}{|l|r|}
\hline
some text & more deep text\\
\hline
\end{tabular}

\begin{tabular}{|l|r|}
\hline
some text & more $\displaystyle\int$ tall text\\
\hline
\end{tabular}
\end{document}

在此处输入图片描述

当然,您可以调整填充。

\documentclass{article} % see https://tex.stackexchange.com/q/42114/121799
% commands based on
\newcommand{\Tstrut}[1]{\rule{0pt}{#1}}       % "top" strut
\newcommand{\Bstrut}[1]{\rule[-#1]{0pt}{0pt}} % "bottom" strut
\newcommand{\TBstrut}[2]{\Tstrut{#1}\Bstrut{#2}}
\begin{document}
\begin{tabular}{|*{20}{c|}}
\hline
some text & more text\\
\hline
\end{tabular}

\begin{tabular}{|*{20}{c|}}
\hline
\TBstrut{2.5ex}{1.5ex}some text & more deep text\\
\hline
\end{tabular}

\begin{tabular}{|*{20}{c|}}
\hline
\TBstrut{4ex}{3ex}some text & more $\displaystyle\int$ tall text & and more \\[0.5ex]
\hline
\end{tabular}

\end{document}

在此处输入图片描述

你可以把它变成一个命令,只要规则少于 20 条即可,例如

\documentclass{article} 
\newcommand{\PartitionedBox}[2][20]{\begin{tabular}{|*{#1}{l|}} \hline #2\\ \hline \end{tabular}}
\begin{document}
 \PartitionedBox{123 & abc & xyz}
\end{document}

有效。(感谢 JouleV 建议使用该选项。)

在此处输入图片描述

相关内容