Tikz 网格直接编译、两次编译和在 IDE 中编译

Tikz 网格直接编译、两次编译和在 IDE 中编译

我的问题已经解决了,但还在研究为什么事情会这样发展。问题与编译过程有关,所以我认为 MWE 不是必需的,但为了以防万一,我在最后提供了一个。这篇文章最后很长,所以提前为这个故事道歉,但我觉得它与我如何陷入当前的困境有关。

背景:我正在尝试通过 Python 设计一个“考试生成器”,它需要一个问题库,选择要包含的问题类型以及考试时间,然后通过选择符合条件的问题并将其导入 xelatex 文档来创建考试和解决方案。然后,它会为用户编译文档,以便他们可以使用 pdf 格式。

大部分文档的创建都没有问题 - 但是,我使用 Tikz 为许多问题创建了一系列网格。当通过 overleaf 和 TeXstudio 运行每个问题时,网格看起来正是我想要的样子。但是,当运行 python 程序时,它们似乎是在标题的某个地方创建的。

我执行的实现此目的的流程是:

xelatex Exam_Qs.tex

为了尝试解决这个问题,我复制了 TeXstudio 正在使用的进程,然后开始运行这个:

xelatex.exe -synctex=1 -interaction=nonstopmode "Exam_Qs".tex

这会导致网格显示在正确的页面中,但现在它们有时会发生偏移。实验表明,总有一个会正确显示,但具体显示哪个并不一致。我通常只使用 IDE,因此我不太习惯以不同的方式运行该过程。我的理解是,nonstopmode 告诉 TeX 引擎尽可能多地运行错误,而 synctex 选项允许 IDE 关联源和 PDF 位置。我确信 synctex 选项对我的最终预期目的没有多大帮助,但当事情不顺利时,完全复制 TeXstudio 过程是有意义的。

真正让我困惑的是,直接运行该过程与在 TeXstudio 内部运行应该至少在我看来是相同的文档。但是,在 TeXstudio 中编译相同的 .tex 文档每次都会生成我期望的网格,而在 python 中调用该过程似乎每次都会使其看起来不正确,并且总是以不同的方式。

在输入完所有这些内容并重新创建我的问题以便能够完整描述它们之后,我最终让 python 运行该过程两次,然后 - 瞧 - 网格的显示与我预期的完全一致。所以,最后,我的问题:

  1. 为什么运行两次该过程会导致文档正确显示/为什么运行一次该过程会导致文档无法正确显示。

  2. 我可以修改我正在调用的流程以便不需要调用它两次吗?

我正在制作的一些网格的 MWE:

\documentclass[11pt,twoside,a4paper]{article}
\usepackage[margin=2cm, headheight=1.25cm, headsep=0.5cm]{geometry}
\usepackage{tikz} % for creating pictures using tikzpicture

\begin{document}

\begin{tikzpicture}[remember picture,overlay]
    \label{grid}
    % Define the size of the grid squares
    \def\minorsize{2mm} % Minor grid size
    \def\majorsize{1cm} % Major grid size
    
    % Calculate the adjusted positions for the grid
    \path (current page.south west) ++(20mm,23mm) coordinate (grid sw); % bottom left point
    \path (current page.north east) ++(-20mm,-23.8mm) coordinate (grid ne); % top right point
    
    % Draw the minor grid
    \draw[black!20, step=\minorsize] (grid sw) grid (grid ne);
    
    % Draw the major grid
    \draw[black!50, step=\majorsize] (grid sw) grid (grid ne);
\end{tikzpicture}\newpage

\vspace{10mm}\begin{tikzpicture}[remember picture, overlay]
    \label{grid}
    % Define the size of the grid squares
    \def\minorsize{2mm} % Minor grid size
    \def\majorsize{1cm} % Major grid size
    
    % Calculate the adjusted positions for the grid
    \path (current page.south west) ++(20mm,25.8mm) coordinate (grid sw); % bottom left point
    \path (current page.north east) ++(-20mm,-41mm) coordinate (grid ne); % top right point
    
    % Draw the minor grid
    \draw[black!20, step=\minorsize] (grid sw) grid (grid ne);
    
    % Draw the major grid
    \draw[black!50, step=\majorsize] (grid sw) grid (grid ne);
\end{tikzpicture}\newpage

\begin{tikzpicture}[remember picture, overlay]
    \label{grid}
    % Define the size of the grid squares
    \def\minorsize{2mm} % Minor grid size
    \def\majorsize{1cm} % Major grid size
    
    % Calculate the adjusted positions for the grid
    \path (current page.south west) ++(20mm,123mm) coordinate (grid sw); % bottom left point
    \path (current page.north east) ++(-20mm,-23.8mm) coordinate (grid ne); % top right point
    
    % Draw the minor grid
    \draw[black!20, step=\minorsize] (grid sw) grid (grid ne);
    
    % Draw the major grid
    \draw[black!50, step=\majorsize] (grid sw) grid (grid ne);
\end{tikzpicture}\vspace{153mm}

\end{document}

(我不希望空间或几何设置影响我遇到的问题,但已将其包括在内,以防万一它再次出现)

相关内容