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.

11
  • Can you please add a minimal example of your usage?
    – Rmano
    Apr 18, 2021 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}} Apr 18, 2021 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, 2021 at 20:14
  • 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]{...}} ??? Apr 18, 2021 at 20:50
  • 1
    @Archange tex.stackexchange.com/a/231741/38080 - example-image-a etc...
    – Rmano
    Apr 18, 2021 at 20:51

3 Answers 3

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).

1
  • 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, 2021 at 21:09
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

7
  • ...except the default \fboxsep is 3pt, not 2pt. Apr 18, 2021 at 18:46
  • 2
    @StevenB.Segletes: I know, but the O.P. asked for 2pt in a display style environment, and -0.5pt for an ‘ordinary’ \fbox..
    – Bernard
    Apr 18, 2021 at 18:47
  • Well, in that case, +1 Apr 18, 2021 at 18:48
  • @daleif: yes, I guess I was out of my mind. Fixed. Thank you for warning me!
    – Bernard
    Apr 18, 2021 at 20:31
  • This does what the OP asks for, but the correct answer is DON'T DO THAT!! \fbox with negative \fboxsep makes no sense at all. Apr 18, 2021 at 20:48
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}}}
4
  • 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, 2021 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. Apr 18, 2021 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, 2021 at 21:15
  • 1
    @Archange clearly I need the rep more than egreg:-) (done:-) Apr 18, 2021 at 21:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .