TikZ 奇怪的默认锚点行为

TikZ 奇怪的默认锚点行为

我在 WSL2 Ubuntu 22.04 上使用从网站下载的 TeX Live 2023。使用 XeTeX 生成 PDF 文档。

xetex -vXeTeX 3.141592653-2.6-0.999995 (TeX Live 2023)

作为计划处理自定义页面大小、利用全纸宽度和使用 TikZ 包的一部分,我从简单的 LaTeX 文档开始。与记录的“tikzpicture”锚点(原点)为西南(左下)的行为相反,我得到了不同的行为。我提供了.tex在我的系统上尝试过的 4 个文件,所有文件都有细微差别。

在以下每个示例中,我尝试在tikzpicture环境中打印两个矩形框,其下方有一些文本。我想使用整页,因此边距设置为 0,并且其中一个框预计从位置 (0,0) 开始,即从纸张的边缘开始。我还习惯在环境\begin{scope} ... \end{scope}中使用相对坐标tikzpicture

对于每个文件,我运行xetex三次命令以确保“记住图片”和“覆盖”按预期工作。

  • (1)tikz-default-notworking.tex:此文件包含 +ve Y 坐标。输出不符合预期,第一个框部分可见,第二个框完全不可见。
\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:与 (1) 类似tikz-default-notworking.tex,但此文件在 中使用了“yscale=-1” tikzpicture。现在,框打印时假设锚点为西北,而不是西南。此外,它从左侧和顶部的某个边缘开始。
\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:与 (1) 类似tikz-default-notworking.tex,但此文件使用 -ve Y 坐标,而不是“yscale=-1”。此文件产生的结果与“yscale=-1”的输出完全相同,即锚点被视为西北方向,(0,0) 从左侧和顶部的一些边缘开始。
\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:此文件明确将tikzpicture锚点设置为西南,并保持与(1)相同的坐标tikz-default-notworking.tex。这次,输出按预期正确生成,框呈现在左下角。此外,这次,(0,0)从页面的左下角开始,没有任何边距。
\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}

我不明白为什么我的系统上的默认锚点行为不同,以及为什么 (0,0) 在默认锚点为“西北”时会留下边距,但在明确设置“锚点 = 西南”时会按预期工作。如果 Stack Exchange 允许上传.tex文件.pdf,我也会上传它们。

答案1

这个例子解释了为什么即使边距已经设置为 ,仍然会有一些间隙0。默认情况下,在页面上输入第一个字母之前会有段落缩进(水平)和顶部跳过(垂直)空间。因此,(0,0)不在页面的西北角。要使(0,0)位于页面的西北角,您需要手动移动到该点(例如,shift={(-\parindent, -\topskip)}yscale=-1或只需使用预定义的页面节点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}

在此处输入图片描述

相关内容