0

I’m using both \fbox and \boxed but for different uses. When I use \fbox, I need it to have \fboxsep set to -0.5pt, but when I used \boxed I want a \fboxsep of 2pt.

Is there a way to do that?

EDIT: As I was asked for my usage, I defined this in my preamble

\setlength\fboxrule{0.5pt}
\setlength\fboxsep{-0.5pt}
\newcommand{\schema}[1]{\fbox{\includegraphics[width=\linewidth]{#1}}}

This is in order to include schemes drawn on a white background in a colored background area (typically one from tcolorbox) and have them neatly separated by a thin black margin.

OTOH, I used \boxed around all types of mathematical formulas.

Archange
  • 1,368
  • Can you please add a minimal example of your usage? – Rmano Apr 18 '21 at 18:22
  • if you set fboxsep negative the left rule will be over-printed by the content and the content will over-print the right rule, but you can define \myboxxed as {\setlength\fboxsep{2pt}\boxed{#1}} – David Carlisle Apr 18 '21 at 18:31
  • @DavidCarlisle If I don’t I get an Overfull \hbox instead, because I use \fbox around figures that are \linewidth wide. See my above edit. – Archange Apr 18 '21 at 20:14
  • @Rmano I’m not sure how to do a MWE with images… Is there something like a default random image for that in LaTeX? – Archange Apr 18 '21 at 20:18
  • @Archange but that makes no sense you are avoiding a warning by destroying the image, and doing that in an asymmetrical way as on some sides the image will be on top and on others the rule will be on top. – David Carlisle Apr 18 '21 at 20:46
  • 1
    why not use the standard approach of making the image fit iinside the box? \noindent\fbox{\gincludegraphics[width=\dimexpr\linewidth-2\fboxsep-2\fboxrule]{...}} ??? – David Carlisle Apr 18 '21 at 20:50
  • 1
    @Archange https://tex.stackexchange.com/a/231741/38080 - example-image-a etc... – Rmano Apr 18 '21 at 20:51
  • @DavidCarlisle Well I’ve never seen any issue with my use, the rule is exactly the same size on all sides. Did not know this was not expected…

    Your solution is mostly fine, excepted I don’t want/need \fboxsep there because this would now be 0pt anyway (I need absolutely no space between the figure and the rule). But thanks for this, I’ll adjust my code with it. :)

    So my original question is still valid, just replace -0.5pt with 0pt and that’s it. ;)

    – Archange Apr 18 '21 at 21:07
  • setting \fboxsep to 0pt is fine but setting it negative does not do anything useful. – David Carlisle Apr 18 '21 at 21:09
  • It made a warning go away while providing the output I wanted (in my case at least), so that’s all I cared about. But anyway, I’ve integrated your much better solution now. :) – Archange Apr 18 '21 at 21:10
  • 0pt is fine and tested setting for fboxsep. – David Carlisle Apr 18 '21 at 21:12

3 Answers3

3

With xpatch:

\documentclass{article} %
\usepackage{amsmath}
\setlength{\fboxsep}{-0.5pt}
\usepackage{xpatch} 
\pretocmd{\boxed}{\setlength{\fboxsep}{2pt}}{}{}

\begin{document}

\[ \boxed{a = \frac{b + c}{2}} \]%

\centering\fbox{$a = \dfrac{b + c}{2}$}

\end{document}

enter image description here

Bernard
  • 271,350
2

You should do it the other way around:

\newcommand{\schema}[1]{%
  \begingroup % localize the changes in the parameters
  \setlength{\fboxsep}{-0.5pt}%
  \setlength{\fboxrule}{0.5pt}%
  \fbox{\includegraphics[width=\linewidth]{#1}}%
  \endgroup
}

This way, any other \fbox (and also \boxed) will use the default values (or the ones you fix for them).

egreg
  • 1,121,712
  • Actually this is much cleaner this way, you are right! Merging this with @DavidCarlisle comments on not using a negative \fboxsep does exactly what I need everywhere. – Archange Apr 18 '21 at 21:09
1

\fbox simply does not work with negative \fboxsep To see what happens see

enter image description here

\documentclass{article}

\usepackage{color}

\begin{document}

\fbox{\textcolor{red}{\rule{1cm}{1cm}}}

\bigskip

\setlength\fboxsep{5pt}

\fbox{\textcolor{red}{\rule{1cm}{1cm}}}

\bigskip

\setlength\fboxsep{-5pt}

\fbox{\textcolor{red}{\rule{1cm}{1cm}}}

\end{document}

The content over-prints the top and left rule and under-prints the right and bottom.

For the actual use case of boxing an image within \textwidth use

\noindent\fbox{%
  \includegraphics[width=\dimexpr\linewidth-2\fboxsep-2\fboxrule]{example-image}}

You could use the above with \fboxsep set to 0pt if you want to space.

To get a local setting with boxed, simplest is to define a custom command that locally sets \fboxsep

\newcommand\myboxed[1]{{{%
    \setlength\fboxsep{2pt}%
     \boxed{#1}}}
David Carlisle
  • 757,742
  • This is not exactly related to the question and the original issue still stand (I need to split boxed and fbox values for fboxsep), so it could have stayed in comments above, but thanks anyway. ;) – Archange Apr 18 '21 at 21:12
  • @Archange well it's related in that the initial values are wrong. I suppose I could add the comment I made originally that you can define \myboxed with a local setting to get a different value there. – David Carlisle Apr 18 '21 at 21:14
  • That would be a good idea indeed, and I would right away upvote your answer too in that case. ;) Not that you need the reputation, but heh. ;p – Archange Apr 18 '21 at 21:15
  • 1
    @Archange clearly I need the rep more than egreg:-) (done:-) – David Carlisle Apr 18 '21 at 21:19