垂直居中 TikZ 图形(\vspace*{fill} 不起作用)

垂直居中 TikZ 图形(\vspace*{fill} 不起作用)

我想打印一张中间有间隙的带线页面,该间隙应垂直位于页面的中心。这是我的代码:

\documentclass[landscape]{article}
\usepackage[paperheight=320mm, paperwidth=450mm, top=0pt, bottom=0pt]{geometry} %Top=-10pt to start right at the top
\usepackage{tikz}

\begin{document}
\newcommand{\linegap}{5}
\newcommand{\linenumber}{19} %Off by one error, actual lines are one more then the one here
\newcommand{\spacing}{25}
\newcommand{\nextparagraphstart}{\the\numexpr\linegap*\linenumber+\spacing}

\begin{figure}
    \begin{tikzpicture}
[
        remember picture,
        overlay,
        every path/.style={
            line width=0.2mm,
        },
    ]
        \foreach \y in {0, \linegap, ..., \the\numexpr\linegap *\linenumber} {
            \coordinate (key) at (0, -\y mm);
            \draw (current page.west |- key) -- (current page.east |- key);
        }

        \foreach \y in {\nextparagraphstart, \the\numexpr\nextparagraphstart+\linegap, ..., \the\numexpr\nextparagraphstart+\linegap*\linenumber} {
            \coordinate (key) at (0, -\y mm);
            \draw (current page.west |- key) -- (current page.east |- key);
        }
    \end{tikzpicture}
\end{figure}
\end{document}

我看过类似的问题,它们建议使用前后图,但没有\vfill任何变化。在和之前使用\null和 ~也没有发生任何变化。我也尝试过前后图,也没有发生任何变化。TikZ 中的坐标命令是否与图形无关,而是特定于页面,还是有其他原因?\vfill\vspace*{\fill}

答案1

通过使用选项remember picture, overlay和页面节点current page,tikz 图片将绘制在当前页面的绝对位置,与周围的间距命令无关,例如\vspace。因此,您应该在所需的位置绘制线条。

\documentclass[landscape]{article}
\usepackage[paperheight=320mm, paperwidth=450mm, top=0pt, bottom=0pt]{geometry} %Top=-10pt to start right at the top
\usepackage{tikz}

\begin{document}
\newlength{\linegap}  \setlength{\linegap}{5mm}
\newlength{\spacing}  \setlength{\spacing}{25mm}
\newlength{\currentyshift}

\newcommand{\linenumber}{19} %Off by one error, actual lines are one more then the one here


\begin{tikzpicture}
[
    remember picture,
    overlay,
    every path/.style={
        line width=0.2mm,
    },
]
    \setlength{\currentyshift}{\dimexpr.5\spacing -\linegap\relax}
    \foreach \y in {0, 1, ..., \linenumber} {
        \global\advance\currentyshift by \linegap
        % or the local assignment
        % \addtolength{\currentyshift}{\y\linegap}
        \draw
          ([yshift= \currentyshift]current page.west) -- 
          ([yshift= \currentyshift]current page.east)
          ([yshift=-\currentyshift]current page.west) -- 
          ([yshift=-\currentyshift]current page.east);
    }
    
    % helper,
    \draw[cyan!50, thick]
      % mark the center of page
      (current page.north west) -- (current page.south east)
      (current page.north east) -- (current page.south west)
      % visualize the spacing
      ([shift={( .5\spacing,  .5\spacing)}]current page) rectangle
      ([shift={(-.5\spacing, -.5\spacing)}]current page);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

[覆盖tikzpicture] 几乎不占用空间(相当于\hbox{})。您需要用空白填充文本区域。否则,任何后续文本都会直接覆盖tikzpicture

我没有尝试修复您的foreach循环。这只是定位页面的中心(和文本区域的中心)。您可以相对于该起点添加线条。

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{figure}[p]
\begin{minipage}[c][\textheight][c]{\textwidth}
\makebox[\textwidth]{center of text area}%
    \begin{tikzpicture}
[
        remember picture,
        overlay,
        every path/.style={
            line width=0.2mm,
        },
    ]
      \node[text=red] at (current page.center) {center of page};
    \end{tikzpicture}
\end{minipage}
\end{figure}
\end{document}

答案3

我只是猜测......像这样的事情?

在此处输入图片描述

(红线位于页面垂直中间,蓝框尺寸等于\spacing毫米)

编辑: 您的代码已大大简化。现在它考虑您的注释(行组之间的距离应等于\spacing)。

与原始解决方案相比,现在少了一个循环,并且计算量减少了\spacing一半\pgfmathsetmacro

我希望现在我能正确地弄清楚一些定义的命令的含义(仍然不清楚哪个单位是什么\linegap)。

\documentclass[landscape]{article}
\usepackage[paperheight=320mm, paperwidth=450mm, 
            top=0pt, bottom=0pt]{geometry}
   \usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\newcommand{\linegap}{5}
\newcommand{\linenumber}{19} %Off by one error, actual lines are one more then the one here
\newcommand{\spacing}{25}

\begin{figure}
    \begin{tikzpicture}[remember picture,overlay]
    \pgfmathsetmacro{\halfspacing}{\spacing/2}
    \draw[red] (current page.west) -- node[draw=blue, inner sep=0pt, minimum size=\spacing mm] {} ++ (\paperwidth,0);

\foreach \y in {0,1, ...,\linenumber}
{
\draw   ([yshift= +\halfspacing mm] $(current page.west)+(0,\y*\linegap/10)$) -- ++ (\paperwidth,0);
\draw   ([yshift= -\halfspacing mm] $(current page.west)-(0,\y*\linegap/10)$) -- ++ (\paperwidth,0);
}
   \end{tikzpicture}
\end{figure}
\end{document}

相关内容