1

I want to push a minipage right at the left border of the page. I understood, that \leftmargin should be the distance between the page border and the text body. So using a negative hspace of that length should push it right to the edge. Yet it doesn't. Is there anything I'm missing?

Also, since the box' width is the same length, shouldn't it end flush with the text body?

MWE:

\documentclass{article}
\usepackage[margin=2cm, a4paper, nomarginpar, hoffset=0pt]{geometry}
\usepackage{showframe}
\usepackage{color}
\definecolor{blackgray}{rgb}{0.25,0.25,0.25}
\setlength{\parindent}{0pt}
\begin{document}
\hspace{-\leftmargin}%
\colorbox{blackgray}{%
        \begin{minipage}{\leftmargin}
                \color{white}test
        \end{minipage}
}
\end{document}

Output output

  • 2
    Remember to extend your example to a full example, document class is missing. – daleif Jan 29 at 9:52
2

\leftmargin is the wrong measure (helpfully...). The left margin is measured as 1 inch, plus the \oddsidemargin (which for an actual 2cm margin will be negative), so this is what you want:

\documentclass{article}
\usepackage[margin=2cm, a4paper, nomarginpar, hoffset=0pt]{geometry}
\usepackage{showframe}
\usepackage{color}
\definecolor{blackgray}{rgb}{0.25,0.25,0.25}
\setlength{\parindent}{0pt}
\begin{document}
\hspace{-\dimexpr1in+\oddsidemargin\relax}%
\colorbox{blackgray}{%
        \begin{minipage}{\dimexpr1in+\oddsidemargin-2\fboxsep\relax}
                \color{white}test
        \end{minipage}
}

\end{document}

Note the width setting for the minipage, which uses the same dimension, but also removes two lots of \fboxsep which is the padding around the text inside the colorbox.

| improve this answer | |
  • Thank you! Do you know the difference between \leftmargin and \oddsidemargin? What does the former consist of? Also thanks for pointing out \fboxsep. I assume this spacing comes with the colorbox? – elumbella Jan 29 at 11:26
  • 1
    \leftmargin is used for indenting lists (and might better have been called \listmargin or similar...), \oddsidemargin is essentially what you want, except that there is this default 1in applied to all margins. Yes, the 2\fboxsep space comes from either side of the \colorbox, around the outside of the minipage. – rbrignall Jan 29 at 11:44

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.