TikZ3 中的絕對定位

TikZ3 中的絕對定位

我也提交了解決方案,請大家節省這個問題的時間和精力。

我的問題是這樣的。將 TikZ2 升級到 TikZ3 後,絕對定位在某些情況下停止工作。這是我期望頁面頂部有一個正方形的程式碼(左圖),但我得到了不同的東西(右圖)。我們如何在 TikZ3 中修復它?

%! pdflatex or xelatex or lualatex
%! bug0002-problem.tex
%! running it twice
\documentclass[a4paper]{article}
\pagestyle{empty}
\addtolength{\hoffset}{-1in}
\addtolength{\voffset}{-1in}
\usepackage{tikz} % version 3
\begin{document}
\begin{tikzpicture}[remember picture, overlay]
\node[minimum width=4cm, minimum height=4cm, draw, fill=orange, anchor=north] at (current page.north) {Hello World!};
\end{tikzpicture}
\end{document}

範例1

答案1

我認為沒有必要明確表達當我看到 TikZ3 中的所有新功能時我是多麼高興,以及當(對我來說)關鍵功能時不時地停止正常工作時我是多麼不高興。

我的第一個解決方案從程式碼本身來看是顯而易見的:註解掉\addtolength{\hoffset}{-1in}\addtolength{\voffset}{-1in}。在實際專案中追蹤它並不是那麼簡單,而且在很久以前設定頁面鏡像的情況下我不可能使用這樣的解決方案。這是代碼。

%! pdflatex or xelatex or lualatex
%! bug0002-solution-a.tex
%! running it twice
% This solution works in xelatex!
\documentclass[a4paper]{article}
\pagestyle{empty}
%\addtolength{\hoffset}{-1in}
%\addtolength{\voffset}{-1in}
\usepackage{tikz} % version 3
\begin{document}
\begin{tikzpicture}[remember picture, overlay]
\node[minimum width=4cm, minimum height=4cm, draw, fill=orange, anchor=north] at (current page.north) {Hello World!};
\end{tikzpicture}
\end{document}

我需要另一個解決方案,我找到了一個:讓我們將\hoffset\voffset尺寸返回到它們的初始值,但僅限於所有環境,tikzpicture因為 TikZ3 顯然使用它們進行計算。請嘗試這個例子,我使用\tikzset並修改了以下樣式every picture

%! pdflatex or xelatex or lualatex
%! bug0002-solution-b.tex
%! running it twice
% This solution does not work in xelatex!
\documentclass[a4paper]{article}
\pagestyle{empty}
\addtolength{\hoffset}{-1in}
\addtolength{\voffset}{-1in}
\usepackage{tikz} % version 3
\begin{document}
\tikzset{every picture/.style={execute at begin picture={
   \hoffset=0pt
   \voffset=0pt
   }}} 
\begin{tikzpicture}[remember picture, overlay]
\node[minimum width=4cm, minimum height=4cm, draw, fill=orange, anchor=north] at (current page.north) {Hello World!};
\end{tikzpicture}
\end{document}

我相信還有更多的事情發生,因為這個解決方案在 中不起作用,但在和xelatex中起作用。我們可以輕鬆比較預期(左圖)和實際輸出(右圖)。恐怕我的答案是提出一個子問題。 TikZ3 中如何解決這個問題?pdflatexlualatexxelatex

範例2

相關內容