Document Classesboxed float that is NOT 3.4pt wider than the textwidth

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
User avatar
seano
Posts: 12
Joined: Tue Jan 29, 2008 3:38 pm

boxed float that is NOT 3.4pt wider than the textwidth

Post by seano »

Hi
I'm sure I'm not the first person to be annoyed by this, but after a relatively long search on the web, battling my way through the index of "The LaTeX Companion" and some DIY effort redifining things in the float.sty file, I still don't have an effective solution to my problem:

How can you put a box around a figure that is alligned with the width of the paragraph?

At first I didn't notice this effect, but now it's really annoying me. I'm actually a bit surprised that everybody seems to have accepted this as OK. But if we're all so happy with a misalignment of 3.4pt every time we put in a picture, why aren't we all just using Microsoft Word?

Anyway, my best effort was to use the following code in the preamble, after loading the float package:

Code: Select all

\makeatletter
\renewcommand\fs@boxed{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@plain
  \def\@fs@pre{\setbox\@currbox\vbox{\hbadness10000
    \moveleft0.0pt\vbox{\advance\hsize by0.8pt
      \hrule \hbox to\hsize{\vrule\kern0.0pt
        \vbox{\kern0.0pt\box\@currbox\kern0.0pt}\kern0.0pt\vrule}\hrule}}}
  \def\@fs@mid{\kern2.0pt}
  \def\@fs@post{}\let\@fs@iftopcapt\iffalse}
\makeatother
This is basically just a copy of the "boxed" style so that the box is only 0.4pt too big (the standard line thickness of the box is 0.4pt).

Does anybody know how to do this properly?
Thanks
Donald E. Knuth said that TeX was intended for the creation of beautiful books.
The plethora of confused, frustrated PhD students trying to use it must therefore be seen as an unintentional side effect.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

boxed float that is NOT 3.4pt wider than the textwidth

Post by localghost »

seano wrote:[...] How can you put a box around a figure that is alligned with the width of the paragraph? [...] Does anybody know how to do this properly? [...]
With the help of the calc package you can try a simple piece of code.

Code: Select all

\begin{figure}[!ht]
  \centering
  \fbox{
    \makebox[\textwidth-4.2\fboxsep-4.2\fboxrule]{
      \rule{6.4cm}{3.6cm}
    }
  }
  \caption{A figure with box aligned with the textwidth}\label{fig:boxfigure}
\end{figure}
Trying this code first, I logically set the width of the box a little bit different.

Code: Select all

\makebox[\textwidth-2\fboxsep-2\fboxrule]{
  \rule{6.4cm}{3.6cm}
}
That normally should do the trick. But it didn't and I don't know why. The box is extended into the margin. So, take the first version. Replace the \rule{6.4cm}{3.6cm} command by whatever you want to put there. This is only a coarse framework, which might be extended. More examples can be found in the document epslatex.


Best regards and welcome on Board
Thorsten
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

boxed float that is NOT 3.4pt wider than the textwidth

Post by Juanjo »

If you write

Code: Select all

\fbox{
    \makebox[\textwidth-2\fboxsep-2\fboxrule]{
      \rule{6.4cm}{3.6cm}
    }
  }
you are leaving spaces before and after \makebox. Please note that it is not the same thing \fbox{Text} than \fbox{ Text } (remark the spaces around "Text" in the last command). A way to make things work is to write

Code: Select all

\fbox{%
    \makebox[\textwidth-2\fboxsep-2\fboxrule]{
      \rule{6.4cm}{3.6cm}
    }%
  }
Anyway, instead of a combination of \fbox and \makebox, one can directly use \framebox. This allows in fact to avoid the use of the calc package:

Code: Select all

\begin{figure}[!ht]
  \centering
  \framebox[\textwidth]{
      \rule{6.4cm}{3.6cm}
    }
  \caption{A figure with box aligned with the textwidth}\label{fig:boxfigure}
\end{figure}
The trick is that the optional argument of \framebox indicates the total width of the box, including the box lines and the separation between frame and content. In other words, \framebox[length]{Text} behaves as \fbox{\makebox[length-2\fboxsep-2\fboxrule]{Text}}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: boxed float that is NOT 3.4pt wider than the textwidth

Post by localghost »

I wonder why this idea did not come to my mind. Sometimes I should think a little less complicated.


Thanks by
Thorsten
User avatar
seano
Posts: 12
Joined: Tue Jan 29, 2008 3:38 pm

boxed float that is NOT 3.4pt wider than the textwidth

Post by seano »

Thanks!
That seems to work much better. However, because I like to make things difficult, I also want to be able to use multiple images within a single box. The code I've used looks like this:

Code: Select all

\begin{figure}[!ht]%
  \framebox[\textwidth]{%
    \parbox[c]{\textwidth}{%
      \subfloat[First big black box]{\rule{14.5cm}{7.0cm}\label{fig_1a}}\\%
      \subfloat[Second similar box ]{\rule{14.5cm}{7.0cm}\label{fig_1b}}\\%
      \subfloat[Third big black box]{\rule{14.5cm}{7.0cm}\label{fig-1c}}}}%
  \caption{Distribution of square boxes}%
  \label{fig:fig_1}%
\end{figure}%
The only problem with this is that it leaves a line of white space between the border and the top of the first figure. I think this is because \parbox begins a new line, but I'm not sure.

Any ideas for a workaround?
Donald E. Knuth said that TeX was intended for the creation of beautiful books.
The plethora of confused, frustrated PhD students trying to use it must therefore be seen as an unintentional side effect.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

boxed float that is NOT 3.4pt wider than the textwidth

Post by localghost »

The whole construction looks a bit more homogeneous when centring the subfigures in the parbox environment. So the top margin doesn't distort the look.

Code: Select all

\begin{figure}[!ht]%
  \framebox[\textwidth]{%
    \parbox{\textwidth}{%
      \centering
      \subfloat[First big black box\label{subfig-1:figure}]{\rule{14.5cm}{7.0cm}}\\%
      \subfloat[Second similar box\label{subfig-2:figure}]{\rule{14.5cm}{7.0cm}}\\%
      \subfloat[Third big black box\label{subfig-3:figure}]{\rule{14.5cm}{7.0cm}}
    }
  }%
  \caption{Distribution of square boxes}\label{fig:figure}
\end{figure}%
You could eliminate that space by using a negative vertical space right after the \centering command.

Code: Select all

\vspace*{-\baselineskip}
User avatar
seano
Posts: 12
Joined: Tue Jan 29, 2008 3:38 pm

boxed float that is NOT 3.4pt wider than the textwidth

Post by seano »

Aaah, that looks better!

Although

Code: Select all

\vspace*{-\baselineskip}\vspace*{1.5pt}
was even better :)

At least, for now it's more accurate. I'm guessing that the \doublespacing that the silly university insists on will muck it all up again...
Never mind. That can be a little task for later.

Thanks for the tip on the centering. My text width was 14.5cm anyway, so I hadn't noticed that they were effectively "misaligned." Fixed now too.
Donald E. Knuth said that TeX was intended for the creation of beautiful books.
The plethora of confused, frustrated PhD students trying to use it must therefore be seen as an unintentional side effect.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

boxed float that is NOT 3.4pt wider than the textwidth

Post by Juanjo »

Assuming that captions are placed below, as it is the case here, the subfig package leaves a small space, called "farskip" above every subfloat. See Figure 91 in page 35 of the manual. The default value of farskip is 10 pt. So, instead of

Code: Select all

\vspace*{-\baselineskip}\vspace*{1.5pt}
it suffices

Code: Select all

\vspace*{-10pt}
Just a final remark. Inside the \framebox there is room only for text having a width less than or equal to \textwidth-2\fboxsep-2\fboxrule. Hence the \parbox is too wide. It is centered in the \framebox, so you notice nothing. In this case, there is no problem, but perhaps things would be different in other situation.
Post Reply