15

I'm using \usepackage[showframe]{geometry} and I'm seeing that when I use a landscape environment (added by pdflscape package) the frame doesn't seem to be OK.

Is this an erratum? Is there any way to correct this?

Here is my MWE:

\documentclass[a4paper,twoside,11pt,openright]{report}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[outer=25mm,inner=35mm,vmargin=20mm,includehead,includefoot,headheight=15pt,showframe]{geometry}
\usepackage{pdflscape}


\begin{document}
Hello.  First page.  Everything ok.

\begin{landscape}
Why is the frame off?
\end{landscape}

\end{document}

And this is the result: enter image description here

lockstep
  • 250,273
Mario S. E.
  • 18,609
  • The problem is that landscape rotates only the textbody. The footer is now on the left and this means the meaning of values like "text width" and "text height" depends on if you are inside or outside the text body: Outside the text height is the length from header to footer and inside the height of the content? Imho would have to patch landscape (so that is adds a marker on landscape pages) and geometry to get a correct result. – Ulrike Fischer May 24 '13 at 10:13
  • @UlrikeFischer I was thinking the same... this smelled like a bug. Do you have any idea if this has already come up before? – Mario S. E. May 24 '13 at 10:14
  • 1
    I would say the word "bug" is a big harsh. landscape is doing some complicated page manipulations things and a simple option like showframe don't have to pay attention to such manipulations. It only would be nice. – Ulrike Fischer May 24 '13 at 10:23
  • @UlrikeFischer I didn't want to be ungrateful, I just don't know how else to call it :P – Mario S. E. May 24 '13 at 10:29
  • @MarioS.E. The word you are looking for is "feature". "bugs" are what you find in other people's packages. – David Carlisle May 24 '13 at 11:18
  • @DavidCarlisle Thanks, that sounds way better!. On other subjects, shouldn't geometry be "aware" of the landscape environment? – Mario S. E. May 24 '13 at 11:22
  • @MarioS.E. well in general two contributed packages written by different authors in different centuries typically don't combine that well automatically, but in this particular case I suspect not. landscape was designed not to affect the page geometry, in particular the page head and foot stay in portrait orientation (which is why \textwidth doesn't change)) It basically just applies \rotatebox{90}{} to the content of each page without changing the page geometry. – David Carlisle May 24 '13 at 11:39
  • @DavidCarlisle Why does \textheight change but not \textwidth? I would expect, based on your description that neither would change. – cfr Apr 19 '14 at 00:31
  • 1
    @cfr I'm not sure I understand your question, lscape makes \textheight the new page height (the old width) but leaves \textwidth as it was so the page head works. (Heiko's answer gives the detail if you ignore the scurrilous comments questioning this design decision:-) – David Carlisle Apr 19 '14 at 23:17
  • @DavidCarlisle So it does change the page geometry. Just it only changes half of it. If it didn't change it, \textheight would also remain unchanged. – cfr Apr 19 '14 at 23:21
  • @cfr well only if you choose to define geometry in that way. What I mean was that lscape is designed to rotate the page body while leaving the page head and foot the same size and position in portrait position. Practically that means changing \textheight (so the output routine splits the text flow for the new "short" length, but keeping \textwitdth the same (as popular packages like fancyhdr use that to set the page head) so that means textwidth and textheight end up being the same, but life's complicated:-) – David Carlisle Apr 19 '14 at 23:25
  • @DavidCarlisle Sadly, yes, it is. Right now, the best work around I have assumes that \textheight and \textwidth are constant throughout the document so that I can save their values to macros at the beginning and count on the saved values for laying out every page, whether in portrait or landscape. If \textheight didn't change in landscape, I could use that value which would be sensitive to changes. Or if \textheight and \textwidth were switched, I could use them appropriately in landscape. The problem is that I can't access the \textheight at all in landscape as far as I can tell. – cfr Apr 20 '14 at 00:08
  • @cfg \linewidth and \columnwidth are both set to the old value of \textheight – David Carlisle Apr 20 '14 at 00:29

1 Answers1

24

There is a "design comprimise" in package lscape (in other packages it would be called "design flaw" ;-), of course). Environment landscape only rotates \textheight and \textwidth remains unchanged. However package geometry expects unchanged values, thus it gets surprised with \textheight having the old value of \textwidth.

The following example fixes this in package geometry's \Gm@vrule that draws the vertical lines. The fixed version uses \gmshow@textheight instead of \textheight. Outside environment landscape the macro \gmshow@textheight expands to \textheight. Inside it expands to \gmshow@@textheight that is initialized with the text height (available in \hsize) at the begin of environment landscape:

\documentclass[a4paper,twoside,11pt,openright]{report}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[
  outer=25mm,
  inner=35mm,
  vmargin=20mm,
  includehead,
  includefoot,
  headheight=15pt,
  showframe
]{geometry}
\usepackage{pdflscape}

\makeatletter
\newcommand*{\gmshow@textheight}{\textheight}
\newdimen\gmshow@@textheight
\g@addto@macro\landscape{%
  \gmshow@@textheight=\hsize
  \renewcommand*{\gmshow@textheight}{\gmshow@@textheight}%
}
\def\Gm@vrule{%
  \vrule width 0.2pt height\gmshow@textheight depth\z@
}%
\makeatother

\begin{document}
\noindent
Hello.  First page.  Everything ok.

\begin{landscape}
  \Huge
  \noindent
  top left\hfill top right\par
  \vfill
  \noindent
  bottom left\hfill bottom right
\end{landscape}
\end{document}

Result

Heiko Oberdiek
  • 271,626
  • 2
    Oi, I saw that:-) – David Carlisle May 28 '13 at 15:04
  • Nice answer! I'll keep the question open just so see if someone else comes with a different approach (I don't want to discourage other answers), but your solution works perfectly. – Mario S. E. May 28 '13 at 21:23
  • Is there a way to adapt this to give the \textheight for use as a length directly? That is, I found this question trying to solve a problem and the showframe issue is a symptom but the main problem is that I get background text in the wrong place. Essentially, I'm trying to answer my own question at http://tex.stackexchange.com/a/172225/39222. I can work around it by saving \textheight and \textwidth but then the values are insensitive to any changes in these values in the document. Plus my solution there feels very kludgey and I am sure it will break in the first whisper of a breeze. – cfr Apr 19 '14 at 00:39
  • Excellent code and solution (I had exactly the same probl...doubt one year later). Plus, this answer and comments in this page explain very well the whole context about landscapes and geometries. – MattAllegro Aug 26 '14 at 17:08