将 tikz 对象与文档坐标系对齐的问题

将 tikz 对象与文档坐标系对齐的问题

我很难将粗线数组与文档的坐标对齐。

下图中可以看到问题。黑框应该与文档中的网格线完美对齐。我在宏中使用的数学公式\arrayBox似乎没有任何对齐问题。有人能帮我解决这个问题吗?

在此处输入图片描述

\documentclass[tikz, border=10mm, letterpaper]{standalone}
\newcommand{\singleBox}[4] % Upper-left corner coordinates (#1,#2), (length,width)=#3,#4
{
    \draw[line width=#4 mm] (#1mm,#2mm-#4 mm *1.0/2.0) -- (#1 mm + #3 mm,#2mm-#4 mm *1.0/2.0);
}

\newcommand{\arrayBox}[6] % Array of single boxes with the spacing between them and the number of boxes needed
{
    \foreach \a in {0,1,...,#6} {
        \def\x{#3 mm + #5 mm}
        \singleBox{#1 mm + \x *\a}{#2}{#3}{#4}
    }

}
\begin{document}
    \begin{tikzpicture}
       \foreach \b in {1,2,...,40} {
          \draw[line width=0.1pt] (\b mm,0) -- (\b mm,30mm);
       }
       \foreach \b in {1,2,...,15} {
          \draw[line width=0.1pt] (0,\b mm) -- (210mm,\b mm);
       }
       \arrayBox{1}{1}{1}{1}{2}{4}
    \end{tikzpicture}
\end{document}

答案1

我不再绘制不同粗细的线条,而是只处理网格坐标。这简化了所有计算。

为此,我修改了\singleBox宏,用该框西北对角线的坐标填充该框:

\newcommand{\singleBox}[4] % Upper-left corner coordinates (#1,#2), (length,depth)=#3,#4
{ \fill(#1mm,#2mm) rectangle (#1 mm + #3 mm,#2mm-#4 mm);
}

我还修改了\arrayBox宏,以便 pgf 在循环中而不是在外部进行坐标计算:

\newcommand{\arrayBox}[6] % Array of single boxes with the spacing between them and the number of boxes needed
{   \foreach \a [evaluate=\a as \debut using (#1+(#3+#5)*\a)] in {0,1,...,#6}   { \singleBox{\debut}{#2}{#3}{#4}
    }
}

结果如下:

截屏

完整代码如下:

\documentclass[tikz, border=5mm, letterpaper]{standalone}
\newcommand{\singleBox}[4] % Upper-left corner coordinates (#1,#2), (length,depth)=#3,#4
{
   % \draw[line width=#4 mm] (#1mm,#2mm-#4 mm/2) -- (#1 mm + #3 mm,#2mm-#4 mm/2);
    \fill(#1mm,#2mm) rectangle (#1 mm + #3 mm,#2mm-#4 mm);
}

\newcommand{\arrayBox}[6] % Array of single boxes with the spacing between them and the number of boxes needed
{   \foreach \a [evaluate=\a as \debut using (#1+(#3+#5)*\a)] in {0,1,...,#6}   { \singleBox{\debut}{#2}{#3}{#4}
    }
}
\begin{document}
    \begin{tikzpicture}
       \foreach \b in {1,2,...,29} {
          \draw[line width=0.1pt] (\b mm,0) -- (\b mm,16mm);
       }
       \foreach \b in {1,2,...,15} {
          \draw[line width=0.1pt] (0,\b mm) -- (30mm,\b mm);
       }
        \arrayBox{1}{1}{1}{1}{2}{4}
        \arrayBox{0}{4}{3}{2}{3}{4}


    \end{tikzpicture}
\end{document}

相关内容