Page LayoutVertical ruler in the margins

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertical ruler in the margins

Post by Cham »

How can I add a vertical line inside the margins, on all the pages of a double-sided document (book class) ?

More specifically, the line should take all the height of the page, and be placed on the left of the first page (odd number), on the right of the second page (even number), and so on. Take note that the line should also appears on the pages with a roman number (i, ii, iii, iv, ... for the ToC and other frontmatter contents).

The line's horizontal distance from the paper's border should be 0.5in.

Here's a mwe, without the vertical line :

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{lipsum}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\chapter{Test chapter one}
\lipsum[1-7]\clearpage

\chapter{Test chapter two}
\lipsum[1-10]

\end{document}
Last edited by Cham on Sat Jun 29, 2013 6:49 pm, edited 2 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Vertical ruler in the margins

Post by localghost »

Cham wrote:[…] More specifically, the line should take all the height of the page, and be placed on the left of the first page (odd number), on the right of the second page (even number), and so on. […]
In short: on all inner margins.

Take a look at this first approach. Further customization possible.

Code: Select all

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
\usepackage[
  total={6in,10in},
  left=1.5in,
  top=0.5in,
  includeheadfoot
]{geometry}
\usepackage{lmodern}
\usepackage{eso-pic}

\newlength{\xoffset}
\newlength{\thickness}
\setlength{\thickness}{0.5in}
\AddToShipoutPicture{%
  \ifodd\value{page}%
    \setlength{\offset}{0pt}%
  \else
    \setlength{\xoffset}{\dimexpr\paperwidth-\thickness}%
  \fi
  \AtPageLowerLeft{%
    \put(\LenToUnit{\xoffset},0){\color{gray!50}\rule{\thickness}{\paperheight}}%
  }%
}

\usepackage{blindtext}

\begin{document}
  \frontmatter
  \tableofcontents

  \mainmatter
  \blinddocument
  \blinddocument
\end{document}
You did not specify the thickness of the rule.


Thorsten
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertical ruler in the margins

Post by Cham »

I'm getting error messages from your code (which isn't working in WriteLaTeX either). \mainmatter not recognised ?

The line should be 1pt in thickness.

EDIT : Apparently, there's a problem in that part (which I don't understand yet). I guess it's a line thickness parameter missing ?

Code: Select all

\newlength{\xoffset}
\newlength{\thickness}
\setlength{\thickness}{0.5in}
\AddToShipoutPicture{%
	\ifodd\value{page}%
	\setlength{\offset}{0pt}%
	\else
	\setlength{\xoffset}{\dimexpr\paperwidth-\thickness}%
  \fi
  \AtPageLowerLeft{%
    \put(\LenToUnit{\xoffset},0){\color{gray!20}\rule{\thickness}{\paperheight}}%
  }%
}

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Vertical ruler in the margins

Post by localghost »

Cham wrote:I'm getting error messages from your code. \mainmatter not recognised ?[…]
But \mainmatter is also in your code. I tested my code and it works.
Cham wrote:[…] The line should be 1pt in thickness.
See this example with the corresponding modifications.

Code: Select all

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
\usepackage[
  total={6in,10in},
  left=1.5in,
  top=0.5in,
  includeheadfoot
]{geometry}
\usepackage{lmodern}
\usepackage{eso-pic}

\newlength{\xoffset}
\newlength{\distance}
\setlength{\distance}{0.5in}
\newlength{\thickness}
\setlength{\thickness}{1pt}
\AddToShipoutPicture{%
  \ifodd\value{page}%
    \setlength{\xoffset}{\distance}%
  \else
    \setlength{\xoffset}{\dimexpr\paperwidth-\thickness-\distance}%
  \fi
  \AtPageLowerLeft{%
    \put(\LenToUnit{\xoffset},0){\rule{\thickness}{\paperheight}}%
  }%
}

\usepackage{blindtext}

\begin{document}
  \frontmatter
  \tableofcontents

  \mainmatter
  \blinddocument
\blinddocument
\end{document}
To see the output click on "Open in writeLaTeX" in the head of the above code box.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Vertical ruler in the margins

Post by Cham »

Ah ! Now it's working great ! :-)

I'll play a little with this, trying to understand how the code is working.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Vertical ruler in the margins

Post by Cham »

Ok, how do you control the vertical position of the line ?

What if we want a 9in line, vertically centered on the page ?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Vertical ruler in the margins

Post by localghost »

Cham wrote:Ok, how do you control the vertical position of the line ?

What if we want a 9in line, vertically centered on the page ?
Simple mathematics.

Would be nice if tell us your wishes all at once and not only step by step.

Code: Select all

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
\usepackage[
  total={6in,10in},
  left=1.5in,
  top=0.5in,
  includeheadfoot
]{geometry}
\usepackage{lmodern}
\usepackage{eso-pic}

\newlength{\distance}
\setlength{\distance}{0.5in}
\newlength{\rulethickness}
\setlength{\rulethickness}{1pt}
\newlength{\ruleheight}
\setlength{\ruleheight}{9in}
\newlength{\xoffset}
\newlength{\yoffset}
\setlength{\yoffset}{0.5\dimexpr\paperheight-\ruleheight}

\AddToShipoutPicture{%
  \ifodd\value{page}%
    \setlength{\xoffset}{\distance}%
  \else
    \setlength{\xoffset}{\dimexpr\paperwidth-\rulethickness-\distance}%
  \fi
  \AtPageLowerLeft{%
    \put(\LenToUnit{\xoffset},\LenToUnit{\yoffset}){\rule{\rulethickness}{\ruleheight}}%
  }%
}

\usepackage{blindtext}

\begin{document}
  \frontmatter
  \tableofcontents

  \mainmatter
  \blinddocument
  \blinddocument
\end{document}
The \AddToShipoutPicture macro is described in the eso-pic manual.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertical ruler in the margins

Post by Cham »

localghost wrote:
Cham wrote:Would be nice if tell us your wishes all at once and not only step by step.
Well, to be honest, I'm not even sure to know what I want/need with this line ! :oops:

Currently, I'm discovering and experimenting with the vertical line. Since the result looks very good, I'll most probably keep it.

But anyway, thanks a lot for your help. It's very appreciated, and I'm sure other users would find this topic usefull.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Vertical ruler in the margins

Post by localghost »

Cham wrote:Well, to be honest, I'm not even sure to know what I want/need with this line ! :oops:

Currently, I'm discovering and experimenting with the vertical line. Since the result looks very good, I'll most probably keep it. […]
OK. That's comprehensible. Feel free to ask follow-up questions if you have a more concrete idea of the final result.
Post Reply