11

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.
Xavier
  • 13,947

1 Answers1

12

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}

enter image description here

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}

enter image description here

Gonzalo Medina
  • 505,128