3

I'm trying to draw a line with TikZ from the top left corner of the text area, but a gap appears both horizontally and vertically. I specified \noindent, but there is still a gap.

\documentclass[a4paper]{article}
\usepackage{geometry}

%---------------------------------------------------------------%
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{tikz}
\setlength{\parindent}{0pt}
\begin{document}
\begin{tikzpicture}

    \node at (0,0) {+};
    \draw (0,0)--(\textwidth,0);

\end{tikzpicture}
\end{document}

EDIT: it should be noted that it is because of the preceding \node at (0,0) {+}; that \draw (0,0)--(\textwidth,0); starting position is displaced from the text area's top left corner.

I'm wondering is there is no way to define a canvas from top left corner to (10, -10) for example, and have robust coordinate system. This way would avoid the use of "overlay" and "remember picture".

In my original document the tikzpicture starts with a rectangle anchored at north and at (.5\textwidth,0), and there are other drawings, and textboxes placed relatively to it. The problem is that the whole tikzpicture is slightly less wide than \textwidth, therefore everything is slightly away from the center, horizontally. TikZ handles the whole tikzpicture like an image and places it in the top left corner of the text area or just next to the preceding object, text...

4
  • isn't it better to globaly position that?A line of width \textwidth plus the space after the tikzpicture is wider than the text width and so things happen. Perhaps you should look up overlay and remember picture options for tikzpicture
    – daleif
    Commented Feb 14, 2022 at 9:04
  • both have coordinates (0,0). Just because of the "+" tikz displaced the actual position with coordinates (0,0). Commented Feb 14, 2022 at 10:41
  • there are numerous ways for "robust", page related coordinates, you can for example use the shipout hooks or the eso-pic package or the tikzpagenodes mentioned below. But normally you don't want to place a picture relative to the page but relative to surrounding text and so that is the default behaviour of tikzpictures. Commented Feb 14, 2022 at 12:54
  • i wanted to avoid tikzpagenodes it makes the code longer. Indeed long sentences like "current page text area.north" just to say (.5\textwidth,0), and on top of that it requires running latex twice. Commented Feb 14, 2022 at 13:47

1 Answer 1

3

Every node has certain size and some outer separation. In following example you can see how your node is well positioned, but the + is not where you want.

As an alternative you can draw using page nodes (with tikzpagenodes package) and forget manual positioning of nodes.

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{tikzpagenodes}

%---------------------------------------------------------------%
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{tikz}
\setlength{\parindent}{0pt}
\begin{document}
\begin{tikzpicture}

    \node[draw, outer sep=0pt] at (0,0) {+};
    \draw (0,0)--(\textwidth,0);

\end{tikzpicture}

\begin{tikzpicture}[overlay, remember picture]
\draw[blue, line width=2pt] (current page text area.north west)--(current page text area.north east);
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .