# Alignment rules

I find myself quite frequently in the need to check whether some elements on my page are aligned exactly as intended. As a result, I usually end up magnifying the output 400% and using a rule over my screen...

Needless to say, I would much rather be able to draw up "alignment rules" from within (La)TeX, but have failed so far to produce an elegant solution. So here is a challenge for people smarter than me, whose result will probably be useful to people more lazy than me :)

Implement "alignment rules" that:

• Extend either horizontally or vertically (2 different commands) from anywhere the commands are issued,
• Extend in both directions up to the page borders,
• Do not interfere with the layout of the page,
• Are displayed in a distinctive color (red), and
• Optionally, provide can be moved up/down (for horizontal alignment rules) or left/right (for vertical alignment rules) by a given distance.
-
Take a look here tex.stackexchange.com/q/41150/14757 – Sigur Apr 25 '13 at 23:44
Papiro unfortunately forgot to add some words after aprovement of his/her edit: Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. – Speravir Apr 26 '13 at 0:05
+1 for using rule ;-) – Harish Kumar Apr 26 '13 at 0:39

One possibility using TikZ; \drawhline draws a horizontal rule and \drawvline, a vertical rule; each command has two optional arguments: the first one allows you to pass options controlling the lines aspect; the second one allows you to specify a vertical shifting for the horizontal line, and a horizontal shifting for the vertical line:

\documentclass{article}
\usepackage{tikz}
\usepackage{twoopt}
\usepackage{lipsum}

\newcounter{line}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newcommandtwoopt\drawvline[2][][0pt]{%
\stepcounter{line}%
\begin{tikzpicture}[remember picture,overlay]
\begin{pgfonlayer}{background}
\coordinate (a\theline);
\draw[red,thick,#1]
([xshift=#2]a\theline|-current page.north) -- ([xshift=#2]a\theline|-current page.south);
\end{pgfonlayer}
\end{tikzpicture}%
}

\newcommandtwoopt\drawhline[2][][0pt]{%
\stepcounter{line}%
\begin{tikzpicture}[remember picture,overlay]
\begin{pgfonlayer}{background}
\coordinate (b\theline);
\draw[red,thick,#1]
([yshift=#2]b\theline-|current page.west) -- ([yshift=#2]b\theline-|current page.east);
\end{pgfonlayer}
\end{tikzpicture}%
}

\begin{document}

\lipsum[4]
Some text\drawvline

\lipsum[4]
Some test text goes here\drawhline[magenta]

\lipsum[4]
Some additional text goes here\drawvline[blue,line width=3pt]\drawhline[blue,line width=3pt]

\lipsum[4]
Text\drawvline[green!40!black][-1cm]\drawhline[green!40!black][-1cm]

\end{document}


The above solution draws rules extending the whole page area; to have then only extending the text area, the tikzpagenodes facilitates the work:

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{tikzpagenodes}
\usepackage{twoopt}
\usepackage{lipsum}

\newcounter{line}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newcommandtwoopt\drawvline[2][][0pt]{%
\stepcounter{line}%
\begin{tikzpicture}[remember picture,overlay]
\begin{pgfonlayer}{background}
\coordinate (a\theline);
\draw[red,thick,#1]
([xshift=#2]a\theline|-current page text area.north) -- ([xshift=#2]a\theline|-current page text area.south);
\end{pgfonlayer}
\end{tikzpicture}%
}

\newcommandtwoopt\drawhline[2][][0pt]{%
\stepcounter{line}%
\begin{tikzpicture}[remember picture,overlay]
\begin{pgfonlayer}{background}
\coordinate (b\theline);
\draw[red,thick,#1]
([yshift=#2]b\theline-|current page text area.west) -- ([yshift=#2]b\theline-|current page text area.east);
\end{pgfonlayer}
\end{tikzpicture}%
}

\begin{document}

\lipsum[4]
Some text\drawvline

\lipsum[4]
Some test text goes here\drawhline[blue,line width=2pt]

\end{document}


-
Fantastic! I really need to learn tikz :) – Xavier Apr 26 '13 at 0:02
@Xavier yes, TikZ is really powerful. By the way, I updated my answer introducing the second optional argument to control shifting and I am not sure if you saw the edit. – Gonzalo Medina Apr 26 '13 at 0:14
I did. I was actually running your code to produce an image for your answer while you were editing it yourself :) – Xavier Apr 26 '13 at 0:22
@Xavier ah, OK :-) – Gonzalo Medina Apr 26 '13 at 0:23
these lines default to full page width/height. How do I adjust the defaults to obey page margins? – blueberryfields Jun 12 '13 at 1:58