Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
\leftskip is set to 0pt for default paragraphs –  Herbert Feb 19 '12 at 13:05
add comment

3 Answers

up vote 10 down vote accepted

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}
share|improve this answer
    
Thanks Marc. I can't believe I forgot about that one... –  Daniel Feb 19 '12 at 11:49
add comment

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}
share|improve this answer
    
\thepage can be something like Page II ... –  Herbert Feb 19 '12 at 12:56
    
@Herbert Thanks for reminding me, just changed to c@page. Would you recommend one does \ifodd\c@page\hbox{}? –  Yiannis Lazarides Feb 19 '12 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 '12 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 '12 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. –  Yiannis Lazarides Feb 19 '12 at 14:07
add comment

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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