Tikzpicture with remember picture and overlay inserting vertical space
The problem
The problem is, like in the questions you link to, that using TikZ switches TeX from vertical to horizontal mode. So if your document starts with a TikZ picture, this picture will be part of the first paragraph of your document, which in your case is the title.
When you use \vspace
in horizontal mode, this space will be inserted after the current line. Here is a little demonstration:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\fbox{\begin{minipage}[t]{2cm} %% no vspace
ab cd ef gh ij kl mn op qr st uv wx yz
\end{minipage}}
%
\fbox{\begin{minipage}[t]{2cm} %% vspace in vertical mode
\vspace{1ex}ab cd ef gh ij kl mn op qr st uv wx yz
\end{minipage}}
%
\fbox{\begin{minipage}[t]{2cm} %% vspace mid paragraph
ab \vspace{1ex} cd ef gh ij kl mn op qr st uv wx yz
\end{minipage}}
%
\fbox{\begin{minipage}[t]{2cm} %% vspace at the start of a paragraph, in horizontal mode
\leavevmode\vspace{1ex}ab cd ef gh ij kl mn op qr st uv wx yz
\end{minipage}}
%
\fbox{\begin{minipage}[t]{2cm} %% vspace after a tikz environment
\begin{tikzpicture}\end{tikzpicture}\vspace{1ex}ab cd ef gh ij kl mn op qr st uv wx yz
\end{minipage}}
\end{document}
(Note that the Tikz picture will reside in a paragraph of its own if it is separated from other content by paragraph breaks, in which case it will probably introduce more vertical space than expected or desired.)
A solution
I think the safer way to include an overlay TikZ environment on a given page is to not have it be part of the running text at all and to draw it when the page is being shipped. That way it is guaranteed not to mess up any spacing.
This can be done by using the atbegshi
package. The following code is mostly identical to yours: I've added five lines and removed one empty line. The changes are indicated by comments.
\documentclass{article}
\usepackage{showframe}
\usepackage{xparse}
\usepackage{tikz}
\usepackage{layout}
\usepackage{atbegshi} %% <-- I added this line
\usepackage[textwidth=5.0cm]{geometry}
\newcommand*{\Text}{%
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.%
}%
\newcommand*{\MyAboveSkip}{5pt}
\newcommand*{\MyBotSkip}{5pt}
\newcommand*{\TypesetTitle}[1]{%
%% See comments in https://tex.stackexchange.com/q/7676/4301
\hbox{}\kern-\topskip
\vspace*{\MyAboveSkip}
{\bfseries\centering#1\par}%
\vspace*{\MyBotSkip}%
}%
\NewDocumentCommand{\TikzDecorations}{%
O{2}% #1 = Number of lines for title
}{%
\AtBeginShipoutNext{% %% <-- I added this line
\AtBeginShipoutUpperLeftForeground{% %% <-- I added this line
\begin{tikzpicture}[remember picture,overlay]
\coordinate (X) at ([
xshift=1.0in+\hoffset+\oddsidemargin,
yshift=-1.0in-\voffset-\topmargin-\headheight-\headsep%
]current page.north west);
% %% <-- Deleted blank line here
\draw [red] ([yshift=-2\baselineskip-\MyAboveSkip-\MyBotSkip]X) -- ++ (\linewidth,0);
\end{tikzpicture}%
}% %% <-- I added this line
}% %% <-- I added this line
}
\begin{document}%\layout
\TikzDecorations[2]
\sloppy\parskip0pt\relax
\TypesetTitle{A Long Title That Requires Two Lines}%
\noindent\Text
\end{document}
Some remarks
I had to delete an empty line in your TikZ environment because
\AtBeginShipoutNext
and\AtBeginShipoutUpperLeftForeground
are not defined\long
and their arguments are therefore not allowed to contain paragraph breaks. (See e.g. this answer.)The TikZ environment inside
\AtBeginShipoutUpperLeftForeground{…}
is placed in the top left corner of the page, so it actually has coordinates(0,0)
. You can therefore replacecurrent page.north west
by0,0
and leave out theremember picture
option. The following thus works just as well:\NewDocumentCommand{\TikzDecorations}{% O{2}% #1 = Number of lines for title }{% \AtBeginShipoutNext{% \AtBeginShipoutUpperLeftForeground{% \begin{tikzpicture}[overlay] %% <-- removed remember picture \coordinate (X) at ([ xshift=1.0in+\hoffset+\oddsidemargin, yshift=-1.0in-\voffset-\topmargin-\headheight-\headsep% ]0,0); %% <-- 0,0 instead of current page.north west \draw [red] ([yshift=-2\baselineskip-\MyAboveSkip-\MyBotSkip]X) -- ++ (\linewidth,0); \end{tikzpicture}% }% }% }
(In fact, you won't even need to run LaTeX twice for this line to show up at the right spot. Note that the opposite corner of the page has coordinates
(\paperwidth,-\paperheight)
, with a minus sign.)If you replace
AtBeginShipoutUpperLeftForeground
byAtBeginShipoutUpperLeft
, then the tikz picture will be drawn behind your text rather than on top of it.
You see exactly the same phenomenon if you just insert an empty box instead of \TikzDecorations[2]
. I would contest that the problem is the first \vspace*
in your \TypesetTitle
macro. Indeed
\documentclass[twoside]{article}
\begin{document}
\showoutput
\mbox{}
\clearpage
\vspace*{0pt}
\noindent
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\clearpage
\mbox{}
\vspace*{0pt}
\noindent
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\end{document}
produces text starting at two different locations
The simplest solution in your case is to move the \TikzDecorations
command to after the title setting command
\documentclass{article}
\usepackage{showframe}
\usepackage{xparse}
\usepackage{tikz}
\usepackage{layout}
\newcommand*{\Text}{%
\sloppy\noindent
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
Curabitur dictum
gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem.
Nulla et lectus vestibulum urna fringilla ultrices.%
}%
\newcommand*{\TitleParShape}{%
0.40\hsize 0.20\hsize
0.35\hsize 0.30\hsize
}%
\newcommand*{\BodyParShape}{%
0.30\hsize 0.40\hsize
0.25\hsize 0.50\hsize
0.20\hsize 0.60\hsize
0.15\hsize 0.70\hsize
0.10\hsize 0.80\hsize
0.05\hsize 0.90\hsize
0.00\hsize 1.00\hsize
}%
\newcommand*{\NumberOfLines}{7 }
\newcommand*{\MyParShape}{%
\parshape \NumberOfLines
\BodyParShape
}%
\newcommand*{\MyAboveSkip}{5pt}
\newcommand*{\MyBotSkip}{5pt}
\newcommand*{\TypesetTitle}[1]{%
%% See comments in https://tex.stackexchange.com/q/7676/4301
\hbox{}\kern-\topskip
\vspace*{\MyAboveSkip}
\parshape 2
\TitleParShape
{\bfseries\centering#1\par}%
\vspace*{\MyBotSkip}%
}%
\NewDocumentCommand{\TikzDecorations}{%
O{2}% #1 = Number of lines for title
}{%
% \noindent
\begin{tikzpicture}[remember picture,overlay]
\coordinate (X) at ([
xshift=1.0in+\hoffset+\oddsidemargin,
yshift=-1.0in-\voffset-\topmargin-\headheight-\headsep%
]current page.north west);
\draw [red] ([yshift=-2\baselineskip-\MyAboveSkip-\MyBotSkip]X) -- ++ (\linewidth,0);
\end{tikzpicture}%
}
\begin{document}%\layout
\sloppy\parskip0pt\relax
\TypesetTitle{Two Line Title}%
\TikzDecorations[2]
\MyParShape
\Text
\end{document}