2

width=\linewidth is acceptable input option to \includegraphics, but is not acceptable as input to minipage. What can we replace X with so that \begin{minipage}{X} has a similair width setting to \includegraphics[width=\linewidth]{file_name}?


The following code is meant to create a framed figure. It almost works except that I do not want the width of the minipage to be hard-coded in to be 12 cm.

\documentclass{article}
\usepackage{graphicx}

\begin{document}\begin{figure}[htbp]
    \centering
    \fbox{
        \begin{minipage}{12 cm}
            \includegraphics[width=\linewidth]{solution_count_satisfiability}
            \caption{caption}
        \label{label}
        \end{minipage}
    }
\end{figure}
\end{document}
  • Off-topic: The \centering instruction is redundant as the minipage is supposed to take up the full width of the textblock. – Mico Jan 29 at 1:40
  • please use example-image as the example so people can run your code. – David Carlisle Jan 29 at 2:39
  • @Mico: The \centering is actually required as the minipage is inside \fbox which makes it a little wider as the line width. – Martin Scharrer Jan 29 at 6:59
  • 1
    @MartinScharrer but \centering does not center boxes that are wider than the line width, it will be overfull on the right margin – David Carlisle Jan 29 at 11:04
  • @MartinScharrer - Indeed, my comment about \centering being redundant was based on the assumption (educate guess?!) that 12cm was the OP's approximation of \textwidth in a LaTeX document that uses the article document class and a font size of 10pt. – Mico Jan 29 at 11:09
1

Not sure what your question is about, simply changing 12cm with \linewidth does it, and, as David already pointed out, you need to subtract the separation and line width of \fbox from it, otherwise the box will be larger then the line width. Also \fbox{<linebreak> will cause a space be added, which will push the image a little to the left, so you need to mask it with a comment: \fbox{%.

The simplest way would be using adjustbox:

\documentclass{article}
\usepackage{adjustbox}% loads graphicx

\usepackage{mwe}% for example text only

\begin{document}

\blindtext

\begin{figure}[htbp]
    \begin{adjustbox}{minipage=\linewidth-2\fboxsep-2\fboxrule,fbox,center}
        \includegraphics[width=\linewidth]{example-image}
        \caption{caption}
        \label{label}
    \end{adjustbox}
\end{figure}

\blindtext

\end{document}

As Mico stated already, the centering (here done with the center key) is not really needed if the content is already as wide as the line. I added it for good measure as you might change the width later.

enter image description here

0

\includegraphics requires the string width= to proceed the width argument. However, minipage does not require that input arguments be labeled. It not only doesn't require it; it doesn't support it. We simply pass in the input argument without a label: \begin{minipage}{\linewidth}

  • 3
    are you going to post this question/answer for every command that takes a length? why just mention minipage not \parbox or tabular* or \makebox ? – David Carlisle Jan 29 at 2:38
  • 2
    If you use \begin{minipage}{\linewidth} in the code in the question the box will be too wide by 2\fboxsep+2\\fboxrule+2 word spaces. – David Carlisle Jan 29 at 2:40

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.