2

I'm using tikz to create a custom title page for my reports. I'd like to draw horizontal lines extending from one edge of the body of the page to the other with the title in between. When I draw only the lines, they do begin at the edge of the page. Adding text shifts them to the right (to align with the center of the text, it appears). How can I get all these objects beginning at the margin? Thanks.

\begin{tikzpicture}
    \draw [line width = 0.3mm, draw = black] (0in,0in) -- (4in,0in);
    \node at (0,-0.75in) {\Huge\bfseries\sffamily\selectfont Title}; 
    \draw [line width = 0.3mm, draw = black] (0,-1.5in) -- (4in,-1.5in);
\end{tikzpicture}
1

The additional text node changes the dimensions of the TikZ picture. It gets wider, so the lines seem to be shifted to the right because the picture starts more at the left, still it needs to be placed in the text area.

Possible solutions:

  • Use overlay for the node so its dimension will not affect the TikZ picture bounding box: \node[overlay] at (0,-0.75in) {\Huge\bfseries\sffamily\selectfont Title};

  • Position the TikZ picture absolutely, you could use the current page node. Quick sample - compile twice:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[overlay, remember picture]
    \draw [line width = 0.3mm, draw = black]
         ($(current page.north west)+( 1in,-1in)$)
      -- ($(current page.north east)+(-3in,-1in)$);
    \node [anchor=west, font=\Huge\bfseries\sffamily]
          at ($(current page.north west)+(1in,-2in)$) {Title}; 
    \draw [line width = 0.3mm, draw = black]
         ($(current page.north west)+( 1in,-3in)$)
      -- ($(current page.north east)+(-3in,-3in)$);
\end{tikzpicture}
\end{document}

output

1

When you're using graphics like Tikz that are going to interact with text, you need to include the overlay option.

If you don't add this option, anything will influence the position of the Tikz figure. Even adding \noindent would create changes.

Output

enter image description here

Code

\documentclass{article}
\usepackage{tikz}

\begin{document}
%Without overlay 
\begin{tikzpicture}
    \draw [line width = 0.3mm, draw = black] (0in,0in) -- (4in,0in);
    \node at (0,-0.75in) {\Huge\bfseries\sffamily\selectfont Title}; 
    \draw [line width = 0.3mm, draw = black] (0,-1.5in) -- (4in,-1.5in);
\end{tikzpicture}
\vspace*{3cm}

% with overlay
\begin{tikzpicture}[overlay]%, remember picture] <-- not necessary in this case.
    \draw [line width = 0.3mm, draw = black] (0in,0in) -- (4in,0in);
    \node at (0,-0.75in) {\Huge\bfseries\sffamily\selectfont Title}; 
    \draw [line width = 0.3mm, draw = black] (0,-1.5in) -- (4in,-1.5in);
\end{tikzpicture}
\end{document}
0

You are right, text nodes in TikZ have their position in the middle, so, if you want to put the title aligned with both lines, you must change the x coordinate to half the length of those lines:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
    \draw [line width = 0.3mm, draw = black] (0in,0in) -- (4in,0in);
    \node at (2in,-0.75in) {\Huge\bfseries\sffamily\selectfont Title};
    \draw [line width = 0.3mm, draw = black] (0in,-1.5in) -- (4in,-1.5in);
\end{tikzpicture}

\end{document}

If you want to make your title wider, keeping the elements centered, you can use this code:

\documentclass{article}
\usepackage{tikz}
%\usepackage[margin=10mm]{geometry}
\begin{document}

\begin{center}
\begin{tikzpicture}
    \newlength{\titleLineWidth}
    % set the title width to 90% of text width
    \setlength{\titleLineWidth}{.9\textwidth}
    \newlength{\titlePosition}
    \setlength{\titlePosition}{\titleLineWidth}
    % put the title just at the middle
    \divide\titlePosition by 2
    \draw [line width = 0.3mm, draw = black] (0in,0in) -- (\titleLineWidth,0in);
    \node at (.5\titleLineWidth,-0.75in) {\Huge\bfseries\sffamily\selectfont Title};
    \draw [line width = 0.3mm, draw = black] (0in,-1.5in) -- (\titleLineWidth,-1.5in);
\end{tikzpicture}
\end{center}

\end{document}

Remove the comment of package geometry to see a wider document, and therefore a wider title.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.