1

I am using TeX Live 2023 downloaded from the site on WSL2 Ubuntu 22.04. Using XeTeX to generate PDF documents.

xetex -v is XeTeX 3.141592653-2.6-0.999995 (TeX Live 2023).

As part of planning to work on custom page size, utilize full paper width and intension to use TikZ package, I started with simple LaTeX document. Contrary to documented behavior of anchor (origin) for "tikzpicture" being south-west (bottom left), I am getting a different behavior. I have provided 4 .tex files I tried on my system, all with minor differences.

In each of the following, I am trying to print two rectangular boxes with some text below it within tikzpicture environment. I would like to use full page hence margin is set to 0 and one of the box is expected to start at position (0,0) ie. from the edges of a paper. I am also using \begin{scope} ... \end{scope} to use relative coordinates within the tikzpicture environment.

For each file, I run xetex command thrice to ensure that "remember picture" and "overlay" works as expected.

  • (1) tikz-default-notworking.tex: This file contains +ve Y coordinate. Output is not as expected, 1st box is partially visible and 2nd box is not visible at all.
\documentclass{article}
\usepackage{tikz}
\usepackage[margin=0mm, paperwidth=210mm, paperheight=297mm]{geometry} % Adjust size as needed
\pagestyle{empty} % Removes page numbers and headers
\begin{document}
\begin{tikzpicture}[remember picture, overlay]
  \begin{scope}[shift={(0cm, 0cm)}] % Positioned partway down the page
    % Draw a rectangle
    \draw (0,0) rectangle ++(4cm,1cm); % Moves downward within the scope
    % Place text below the rectangle
    \node at (2cm, 1.5cm) {Below First rectangle}; % Further down (more negative Y)
  \end{scope}
\end{tikzpicture}
\begin{tikzpicture}[remember picture, overlay]
  \begin{scope}[shift={(5cm, 5cm)}] % Positioned partway down the page
    % Draw a rectangle
    \draw (0,0) rectangle ++(4cm,2cm); % Moves downward within the scope
    % Place text below the rectangle
    \node at (2cm, 3cm) {Below Second rectangle}; % Further down (more negative Y)
  \end{scope}
\end{tikzpicture}
\end{document}
  • (2) tikz-explicit-yscale.tex: Similar to (1) tikz-default-notworking.tex but this file use "yscale=-1" in tikzpicture. Now, boxes are printed assuming anchor as north-west, not south-west. Moreover, it starts from some margin on left and top.
\documentclass{article}
\usepackage{tikz}
\usepackage[margin=0mm, paperwidth=210mm, paperheight=297mm]{geometry} % Adjust size as needed
\pagestyle{empty} % Removes page numbers and headers
\begin{document}
\begin{tikzpicture}[remember picture, overlay, yscale=-1]
  \begin{scope}[shift={(0cm, 0cm)}] % Positioned partway down the page
    % Draw a rectangle
    \draw (0,0) rectangle ++(4cm,1cm); % Moves downward within the scope
    % Place text below the rectangle
    \node at (2cm, 1.5cm) {Below First rectangle}; % Further down (more negative Y)
  \end{scope}
\end{tikzpicture}
\begin{tikzpicture}[remember picture, overlay, yscale=-1]
  \begin{scope}[shift={(5cm, 5cm)}] % Positioned partway down the page
    % Draw a rectangle
    \draw (0,0) rectangle ++(4cm,2cm); % Moves downward within the scope
    % Place text below the rectangle
    \node at (2cm, 3cm) {Below Second rectangle}; % Further down (more negative Y)
  \end{scope}
\end{tikzpicture}
\end{document}
  • (3) tikz-default-as-north-west.tex: Similar to (1) tikz-default-notworking.tex but this file use -ve Y coordinate instead of "yscale=-1". This file produces exactly same as output like "yscale=-1" ie. anchor is treated as north-west and (0,0) starts with some margin on left and top.
\documentclass{article}
\usepackage{tikz}
\usepackage[margin=0mm, paperwidth=210mm, paperheight=297mm]{geometry} % Adjust size as needed
\pagestyle{empty} % Removes page numbers and headers
\begin{document}
\begin{tikzpicture}[remember picture, overlay]
  \begin{scope}[shift={(0cm, 0cm)}] % Positioned partway down the page
    % Draw a rectangle
    \draw (0,0) rectangle ++(4cm,-1cm); % Moves downward within the scope
    % Place text below the rectangle
    \node at (2cm, -1.5cm) {Below First rectangle}; % Further down (more negative Y)
  \end{scope}
\end{tikzpicture}
\begin{tikzpicture}[remember picture, overlay]
  \begin{scope}[shift={(5cm, -5cm)}] % Positioned partway down the page
    % Draw a rectangle
    \draw (0,0) rectangle ++(4cm,-2cm); % Moves downward within the scope
    % Place text below the rectangle
    \node at (2cm, -3cm) {Below Second rectangle}; % Further down (more negative Y)
  \end{scope}
\end{tikzpicture}
\end{document}
  • (4) tikz-explicit-south-west.tex: This file explicitly sets tikzpicture anchor to south-west and maintains same coordinates as (1) tikz-default-notworking.tex. This time, output is correctly produced as expected, boxes are rendered at the bottom left. Moreover, this time, (0,0) starts with bottom-left corner of a page, without any margin.
\documentclass{article}
\usepackage{tikz}
\usepackage[margin=0mm, paperwidth=210mm, paperheight=297mm]{geometry} % Adjust size as needed
\pagestyle{empty} % Removes page numbers and headers
\begin{document}
\begin{tikzpicture}[remember picture, overlay, shift={(current page.south west)}]
  \begin{scope}[shift={(0cm, 0cm)}] % Positioned partway down the page
    % Draw a rectangle
    \draw (0,0) rectangle ++(4cm,1cm); % Moves downward within the scope
    % Place text below the rectangle
    \node at (2cm, 1.5cm) {Below First rectangle}; % Further down (more negative Y)
  \end{scope}
\end{tikzpicture}
\begin{tikzpicture}[remember picture, overlay, shift={(current page.south west)}]
  \begin{scope}[shift={(5cm, 5cm)}] % Positioned partway down the page
    % Draw a rectangle
    \draw (0,0) rectangle ++(4cm,2cm); % Moves downward within the scope
    % Place text below the rectangle
    \node at (2cm, 3cm) {Below Second rectangle}; % Further down (more negative Y)
  \end{scope}
\end{tikzpicture}
\end{document}

I fail to understand why default anchor behavior is different on my system, and why (0,0) leaves a margin in case default anchor is "north-west" but works as expected when "anchor = south-west" is explicitly set. If stack exchange permits uploading .tex and .pdf files, I will upload those as well.

2
  • 1
    The way you think where is point (0,0) was not the correct way tikz will think. tikz will always treat the bottom left corner of your current input location as (0,0). Any shifts you set will be followed as: positive x to the right and positive y to top, vice versa. Take a look this post: tex.stackexchange.com/a/642203/267375
    – Tom
    Feb 16 at 21:17
  • 1
    Also, I think you forget there will be indent at beginning of the paragraph by default if paragraph was not directly after a section heading.
    – Tom
    Feb 16 at 21:24

1 Answer 1

2

This is an example to explain why there are some gaps even the margin was already set to 0. By default, there will be paragraph indent (horizontally) and topskip (vertical) space before the first letter been type out on a page. So, the (0,0) was not on the page north west corner. To make (0,0) on the page north west corner, you need manually shift to that point (e.g. shift={(-\parindent, -\topskip)} when yscale=-1 or just simply use the predefined the page node shift={(current page.north west)}).

\documentclass{article}
\usepackage{tikz}
\usepackage[margin=0mm, paperwidth=210mm, paperheight=297mm]{geometry}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[remember picture, overlay, yscale=-1]
\draw [<-] (0,0) -- (1cm,1cm) node [anchor=north west,,inner sep=0pt] {This is the location of (0,0) outside the scope};
\begin{scope}[shift={(-\parindent, -\topskip)}] 
\draw [<-] (0,0) -- (1.5cm,2cm) node [anchor=north west,inner sep=0pt] {This is the location of (0,0) inside the scope}; 
\end{scope}
\end{tikzpicture}text start here...
\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 .