8

How LaTeX calculates margins has already seemed somewhat complicated to me. In my attempt to improve my answer to this question about how to have a graphics span the complete paper with I tried again to understand it – and after reading the answer of TH to this question I thought I got it how the left margin is calculated:

\hoffset + 1in + \oddsidemargin + \leftskip

My expectation was that if I skip the given amount to the left, I will end up at the real paper margin. However, apparently that does not work:

\documentclass{article}
\usepackage{lipsum}
\begin{document}
  \lipsum[3]
  \hskip\dimexpr -\hoffset-1in-\oddsidemargin-\leftskip\relax\rule{\paperwidth}{4pt}
  \lipsum[3]
\end{document}

This gives the following result:

enter image description here

So there is still some space between the left-hand paper margin and the output of the \rule command. So which length is still missing?

1
  • \leftskip is set to 0pt for default paragraphs
    – user2478
    Feb 19, 2012 at 13:05

3 Answers 3

10

After a paragraph break, LaTeX inserts a \parindent at the start of the next paragraph. To suppress this \parindent, you should insert a \noindent at the start of the next paragraph.

\documentclass{article}
\usepackage{lipsum}
\begin{document}
 \lipsum[3]
 \noindent\hskip\dimexpr -\hoffset-1in-\oddsidemargin-\leftskip\relax\rule{\paperwidth}{4pt}
  \lipsum[3]
\end{document}
0
7

Just to expand on Marc's fine answer. You should also check for even or odd pages, as your code even with parindent0pt will fail on even pages rather use:

\documentclass{article}
\usepackage{lipsum}
\parindent0pt
\begin{document}
\makeatletter
\ifodd\c@page 
  \lipsum[3]
  \hskip\dimexpr -\hoffset-1in-\oddsidemargin-\leftskip\relax\rule{\paperwidth}{4pt}
  \lipsum[3]
\else
\the\leftskip
\newpage
 \lipsum[3]
  \hskip\dimexpr -\hoffset-1in-\evensidemargin-\leftskip\relax\rule{\paperwidth}{4pt}
  \lipsum[3]
\fi
\makeatother
\end{document}
5
  • \thepage can be something like Page II ...
    – user2478
    Feb 19, 2012 at 12:56
  • @Herbert Thanks for reminding me, just changed to c@page. Would you recommend one does \ifodd\c@page\hbox{}?
    – yannisl
    Feb 19, 2012 at 13:18
  • One can more simply use \value{page} after \ifodd. However this might give wrong results if the construction happens to fall near to a page break: the used value of page may be off by one with respect to where the text is eventually printed.
    – egreg
    Feb 19, 2012 at 13:39
  • Detecting odd/even reliably seems to be nontrivial at best. I finally came up with a solution based on the changepage package and the \strictpagecheck option: tex.stackexchange.com/a/43810/3751
    – Daniel
    Feb 19, 2012 at 13:58
  • @Daniel I normally use changepage as well. Here I was not worried, as the example was distinctly checking just after a newpage. Test will also fail if the page counter is undefined.
    – yannisl
    Feb 19, 2012 at 14:07
4

this works for one and two side documents. It needs two LaTeX runs

\documentclass[twoside]{article}
\usepackage{lipsum}
\newcounter{rulecounter}
\makeatletter
\newcommand*\longrule[1][4pt]{%
  \refstepcounter{rulecounter}\label{longrule-label@\therulecounter}%
  \par\noindent
  \ifodd\pageref{longrule-label@\therulecounter}
    \hskip\dimexpr-\hoffset-1in-\oddsidemargin-\leftskip\relax
  \else
    \hskip\dimexpr-\hoffset-1in-\evensidemargin-\leftskip\relax
  \fi%
  \rule{\paperwidth}{#1}\par}
\makeatother
\begin{document}
\lipsum[3]
\longrule
\lipsum[3]
\newpage
\lipsum[3]
\longrule[6pt]
\lipsum[3]
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .