[Tex/LaTex] Fbox increase the spacing between the box and it content (inner margin)

boxesframedspacing

Is there a way to control the top and bottom spacing between the box and it content (text)? and secondary about left and right spacing? in other words i am looking for the analogous of \arraystretch command in this case.

PS: here \parbox is used to allow line breaking.

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

Best Answer

By default, \fbox and the underlying \fr@meb@x use a single length \fboxsep for the separation of the box content from the box margin (and rule) -- This is easier of course and is also connected to the fact that in earlier times there weren't much length registers at hand just to squander them.

I've stolen the code from latex.ltx and changed the settings, using \fboxhsep and \fboxvsep, at the right places, to provide a solution without any extra package.

Of course, this \fboxother won't break over pages. For such things tcolorbox etc. is much easier.

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

Update

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

enter image description here