[Tex/LaTex] Fbox’ed minipages with fboxsep=0pt, fboxrule=0.0001pt and colored border

boxescolorminipage

I have arranged within approx. 20 minipages a data plot, several pictures with attached labels taking columnwidth, columnsep of the twocolumn paper format etc into account.

For debugging purposes, I have surrounded each minipage with a \cfbox command to see the width and height of the minipages, unintended whitespace, proper alignment w.r.t. each other.

Update:

The resulting pdf-document looks like this:

enter image description here

The source code is:

\documentclass[class=elsarticle,preprint,5p,10pt,twocolumn]{standalone}
\usepackage{mwe}
\usepackage{calc}
\usepackage{xcolor}

\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{0.001pt}

\newcommand{\cfbox}[1]{%
  \colorlet{currentcolor}{.}%
  {\color{red}%
  \fbox{\color{currentcolor}#1}}%
}

\newlength{\mywidth}
\setlength{\mywidth}{3.2in}
\newlength{\myheight}
\setlength{\myheight}{3.2in}

\begin{document}
\cfbox{\begin{minipage}[t][\myheight][b]{\mywidth}
  \cfbox{\includegraphics[width=\mywidth, height=\myheight]{plot}}
\end{minipage}}%
\cfbox{\begin{minipage}[t][\myheight][c]{\mywidth} % currently square (not necessarily)
  \cfbox{\begin{minipage}[t][\myheight*\real{0.125}][b]{\textwidth} % gnuplot tmargin
    \centering
    Series 1\vspace{\lineskip}
  \end{minipage}}\\[-\lineskip]
  \cfbox{\begin{minipage}[t][\myheight*\real{0.75}/\real{3}]{\textwidth}
    \cfbox{\begin{minipage}{\textwidth/\real{2}}
      \centering
      \cfbox{\includegraphics[width=0.4\textwidth]{example-image-a}}

      \scriptsize{}label-a
    \end{minipage}}%
    \cfbox{\begin{minipage}{\textwidth/\real{2}}
      \centering
      \cfbox{\includegraphics[width=0.4\textwidth]{example-image-b}}

      \scriptsize{}label-b
    \end{minipage}}%
  \end{minipage}}\\[-\lineskip]
  \cfbox{\begin{minipage}[t][\myheight*\real{0.75}/\real{3}][c]{\textwidth}
    \centering
    Series 2\vspace{\lineskip}
    \cfbox{\begin{minipage}{\textwidth/\real{2}}
      \centering
      \cfbox{\includegraphics[width=0.4\textwidth]{example-image-a}}

      \scriptsize{}label-a
    \end{minipage}}%
    \cfbox{\begin{minipage}{\textwidth/\real{2}}
      \centering
      \cfbox{\includegraphics[width=0.4\textwidth]{example-image-b}}

      \scriptsize{}label-b
    \end{minipage}}%
  \end{minipage}}\\[-\lineskip]
  \cfbox{\begin{minipage}[t][\myheight*\real{0.75}/\real{3}]{\textwidth}
    \vfill
    \centering
    Series 3\vspace{\lineskip}
    \cfbox{\begin{minipage}{\textwidth/\real{2}}
      \centering
      \cfbox{\includegraphics[width=0.4\textwidth]{example-image-a}}
    \end{minipage}}%
    \cfbox{\begin{minipage}{\textwidth/\real{2}}
      \centering
      \cfbox{\includegraphics[width=0.4\textwidth]{example-image-b}}
    \end{minipage}}%
  \end{minipage}}\\[-\lineskip]
  \cfbox{\begin{minipage}[t][\myheight*\real{0.125}]{\textwidth} % gnuplot bmargin
    \vspace{\lineskip}
    \cfbox{\begin{minipage}{\textwidth/\real{2}}
      \centering
      \scriptsize{}label-a
    \end{minipage}}%
    \cfbox{\begin{minipage}{\textwidth/\real{2}}
      \centering
      \scriptsize{}label-b
    \end{minipage}}%
  \end{minipage}}
\end{minipage}}%
\end{document}

I consider this source code as messy.

I'd like to remove the debugging code from the body of the document, i.e. remove the \cfbox within \begin{document} and \end{document}, and re-define the minipage command in the preamble.

However, I could not find a solution adding colored boxes to minipages without introducing additional whitespace.

I have considered:

  • package framed
  • package boxedminipage
  • renewenvironment minipage with xparse

I hope the update explains that the visible borders are meant to be only temporary. They are not in the final document. Currently, I remove them by redefining \cfbox by \renewcommand{\cfbox}[1]{#1}

Partially working solution based on the answer by @touhami and @egreg's answer on renew minipage with xparse

\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{0.0001pt}
\setlength{\parindent}{0pt}
\newbox\mtbox
\let\latexminipage\minipage
\let\latexendminipage\endminipage
\colorlet{currentcolor}{.}
\RenewDocumentEnvironment{minipage}{ooom}
 {%
  \IfNoValueTF{#1}
   {\setbox\mtbox\vbox\bgroup\hsize=#4
     \latexminipage{\hsize}}
   {%
    \IfNoValueTF{#2}
   {\setbox\mtbox\vbox\bgroup\hsize=#4
     \latexminipage[#1]{#4}}
     {%
      \IfNoValueTF{#3}
   {\setbox\mtbox\vbox\bgroup\hsize=#4
       \latexminipage[#1][#2]{#4}}
   {\setbox\mtbox\vbox\bgroup\hsize=#4
       \latexminipage[#1][#2][#3]{#4}}%
     }%
   }%
 }
 {\latexendminipage\egroup%
\color{red}\fbox{\color{currentcolor}\box\mtbox}}

\newcommand{\mylipsum}{Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis.}
\begin{document}
\begin{minipage}[t][1in]{5cm}
\mylipsum
\end{minipage}
\end{document}

However, setting a minipage width or height by \begin{minipage}[t][\myheight*\real{0.75}/\real{3}]{\textwidth}, a wrong measure is applied. This has probably something to do with the xparse package based redefinition of the minipage environment. Answer by egreg does not have this issue.

Best Answer

My impression is that you're overcomplicating things.

\documentclass{article}
\usepackage{graphicx,xcolor}

\newsavebox{\cfboxbox}
\newcommand{\cfbox}[1]{%
  \mbox{%
    \sbox{\cfboxbox}{#1}%
    \setlength{\fboxsep}{-0.0001pt}% don't add space
    \setlength{\fboxrule}{0.0001pt}%
    \color{red}%
    \fbox{\usebox{\cfboxbox}}%
  }%
}

\begin{document}

\cfbox{\includegraphics[width=3cm]{example-image}}

\cfbox{\textcolor{blue}{Hello world}}

\end{document}

enter image description here

When you have finished debugging, just redefine \cbox to do nothing:

\newcommand{\cbox}[1]{#1}

If you don't want \cbox in the way, you can redefine minipage and \includegraphics to do it implicitly:

\documentclass{article}
\usepackage{graphicx,xcolor,calc}


\let\latexminipage\minipage
\let\endlatexminipage\endminipage
\let\latexincludegraphics\includegraphics

\newsavebox{\cfboxbox}

\newcommand{\redbox}[1]{%
  \mbox{%
    \setlength{\fboxsep}{-0.0001pt}%
    \setlength{\fboxrule}{0.0001pt}%
    \color{red}%
    \fbox{#1}%
  }%
}

\renewenvironment{minipage}
 {\begin{lrbox}{\cfboxbox}\begin{latexminipage}}
 {\end{latexminipage}\end{lrbox}\redbox{\usebox{\cfboxbox}}}

\renewcommand{\includegraphics}[2][]{%
  \redbox{\latexincludegraphics[#1]{#2}}%
}

\newlength{\mywidth}
\setlength{\mywidth}{2in}
\newlength{\myheight}
\setlength{\myheight}{2in}

\begin{document}

\begin{minipage}[t][\myheight][b]{\mywidth}
  \includegraphics[width=\mywidth, height=\myheight]{example-image}
\end{minipage}%
\begin{minipage}[t][\myheight][c]{\mywidth} % currently square (not necessarily)
  \begin{minipage}[t][\myheight*\real{0.125}][b]{\textwidth} % gnuplot tmargin
    \centering
    Series 1\vspace{\lineskip}
  \end{minipage}\\[-\lineskip]
  \begin{minipage}[t][\myheight*\real{0.75}/\real{3}]{\textwidth}
    \begin{minipage}{\textwidth/\real{2}}
      \centering
      \includegraphics[width=0.4\textwidth]{example-image-a}

      \scriptsize{}label-a
    \end{minipage}%
    \begin{minipage}{\textwidth/\real{2}}
      \centering
      \includegraphics[width=0.4\textwidth]{example-image-b}

      \scriptsize{}label-b
    \end{minipage}%
  \end{minipage}
\end{minipage}

\end{document}

enter image description here

If you have other constructions that should have an implicit frame, you have to cope with them separately in a similar fashion.