Fbox 增加框与其内容之间的间距(内边距)

Fbox 增加框与其内容之间的间距(内边距)

有没有办法控制框和其内容(文本)之间的顶部和底部间距?其次是左右间距?换句话说,我正在寻找\arraystretch这种情况下命令的类似物。

PS:这里\parbox用于允许换行。

\documentclass{article}
\begin{document}
\fbox{\parbox{\linewidth}{Hello world test example break this line\\
here is a new line just for the example}}
\end{document}

答案1

默认情况下,\fbox底层\fr@meb@x使用单一长度\fboxsep来区分框内容和框边距(和规则)——这当然更容易,也与早期没有太多的长度寄存器可用而只能浪费它们有关。

我从中窃取了代码latex.ltx并更改了设置,在正确的位置使用\fboxhsep\fboxvsep,以提供无需任何额外包的解决方案。

当然,这\fboxother不会跨页。对于这样的事情tcolorbox等来说要容易得多。

\documentclass{article}

\makeatletter

\newlength{\fboxhsep}
\newlength{\fboxvsep}

\setlength{\fboxhsep}{5\fboxsep}
\setlength{\fboxvsep}{20\fboxsep}


\def\@frameb@xother#1{%
  \@tempdima\fboxrule
  \advance\@tempdima\fboxvsep
  \advance\@tempdima\dp\@tempboxa
  \hbox{%
    \lower\@tempdima\hbox{%
      \vbox{%
        \hrule\@height\fboxrule
        \hbox{%
          \vrule\@width\fboxrule
          #1%
          \vbox{%
            \vskip\fboxvsep
            \box\@tempboxa
            \vskip\fboxvsep}%
          #1%
          \vrule\@width\fboxrule}%
        \hrule\@height\fboxrule}%
    }%
  }%
}


\long\def\fboxother#1{%
  \leavevmode
  \setbox\@tempboxa\hbox{%
    \color@begingroup
    \kern\fboxhsep{#1}\kern\fboxhsep
    \color@endgroup}%
  \@frameb@xother\relax}

\makeatother
\begin{document}
\fboxother{\parbox{\linewidth}{Hello world test example break this line\\
here is a new line just for the example}}
\end{document}

更新

\documentclass{article}

\makeatletter

\newlength{\fboxhsep}
\newlength{\fboxvsep}

\newlength{\fboxtoprule}
\newlength{\fboxbottomrule}
\newlength{\fboxleftrule}
\newlength{\fboxrightrule}



\setlength{\fboxhsep}{5\fboxsep}
\setlength{\fboxvsep}{20\fboxsep}

\setlength{\fboxtoprule}{\fboxrule}
\setlength{\fboxleftrule}{\fboxrule}
\setlength{\fboxrightrule}{\fboxrule}
\setlength{\fboxbottomrule}{\fboxrule}



\def\@frameb@xother#1{%
  \@tempdima\fboxtoprule
  \advance\@tempdima\fboxvsep
  \advance\@tempdima\dp\@tempboxa
  \hbox{%
    \lower\@tempdima\hbox{%
      \vbox{%
        \hrule\@height\fboxtoprule
        \hbox{%
          \vrule\@width\fboxleftrule
          #1%
          \vbox{%
            \vskip\fboxvsep
            \box\@tempboxa
            \vskip\fboxvsep}%
          #1%
          \vrule\@width\fboxrightrule}%
        \hrule\@height\fboxbottomrule}%
    }%
  }%
}


\long\def\fboxother#1{%
  \leavevmode
  \setbox\@tempboxa\hbox{%
    \color@begingroup
    \kern\fboxhsep{#1}\kern\fboxhsep
    \color@endgroup}%
  \@frameb@xother\relax}

\newcommand{\myfbox}[3]{%
  \begingroup
  \setlength{\fboxhsep}{#1}
  \setlength{\fboxvsep}{#2}
  \fboxother{#3}

  \endgroup
}

\makeatother
\begin{document}
\fboxother{\parbox{\linewidth}{Hello world test example break this line\\
here is a new line just for the example}}
\myfbox{1cm}{1.5cm}{\parbox{\linewidth}{Hello world test example break this line \\
here is a new line just for the example}}
\end{document}

在此处输入图片描述

相关内容