1

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.

2
  • 1
    Can you elaborate the intended goal? Do you want a colored box around the image with no whicte space? Why the use of minipages? Are you aware of tcolorbox?
    – user11232
    Aug 5 '15 at 16:26
  • I support Harish's idea of tcolorbox -- There's no need of redefining minipage. tcolorbox can be a Swiss knife ;-)
    – user31729
    Aug 5 '15 at 18:28
3

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.

1
  • Thanks for your update/edit. That's it what I was looking for. Possibly using a DTP program (e.g. scribus) for such a task of layouting with non trivial alignments is more user friendly. However, it's nice to be able to do it within the same environment as writing.
    – Hotschke
    Aug 6 '15 at 9:23
2

Something like this enter image description here

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

\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{0.0001pt}
\setlength{\parindent}{0pt}

\newbox\mtbox
\newenvironment{myminipage}[1]%
{\setbox\mtbox\vbox\bgroup\hsize=#1
\begin{minipage}{\hsize}}%
{\end{minipage}\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}

\colorlet{currentcolor}{.}
\begin{myminipage}{5cm}
\mylipsum
\end{myminipage}

\mylipsum

\begin{myminipage}{\columnwidth}
\includegraphics{example-image}
\end{myminipage}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.