如何将方框置于纸张的正中央?

如何将方框置于纸张的正中央?

我正在尝试在当前页面(A4 尺寸)的中心绘制一个框。我尝试以下代码

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,trees,shapes.misc}   

\begin{document}
  \pagestyle{empty}
  \begin{tikzpicture}
    \begin{scope}[color=gray,line width=1pt]
      \draw (-15cm,0) -- (1cm,0) -- (1cm,16cm) -- (-15cm,16cm) -- cycle;
    \end{scope}
  \end{tikzpicture}
\end{document}

现在,我手动更改了框角的坐标,但这没有帮助。它实际上并没有位于页面的中心。

答案1

您可以使用current page.center节点:

\documentclass{article}
\usepackage{tikz}

\begin{document}
  \pagestyle{empty}
  \begin{tikzpicture}[remember picture,overlay]
    \begin{scope}[color=gray,line width=1pt]
\foreach \shift in {1cm,2cm,3cm,4cm,5cm}
      \draw ([xshift=-\shift,yshift=\shift]current page.center) rectangle ([xshift=\shift,yshift=-\shift]current page.center);
    \end{scope}
\node at (current page.center) {a};
  \end{tikzpicture}
\end{document}

在此处输入图片描述

上述代码将生成以 为中心的正方形current page.center,即从纸张左上角到右下角的连线与从左下角到右上角的连线的交点;如果您希望它们位于“文本区域中心”的中心,那么您可以使用 和它的节点;这tikzpagenodes两个current text area.center节点可能不同,如以下示例所示;红色圆圈的中心为current page.center,绿色圆圈的中心为current page text area.center(在a4paper尺寸上差异不是很大,但仍然很明显):

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{tikzpagenodes}

\pagestyle{empty}

\begin{document}

\begin{tikzpicture}[remember picture,overlay]
\draw[red] (current page.north west) -- (current page.south east);
\draw[red] (current page.north east) -- (current page.south west);
\node[draw,circle,red] at (current page.center) {};

\begin{scope}[color=gray,line width=1pt]
\foreach \shift in {1cm,2cm,3cm,4cm,5cm,6cm}
  \draw 
  ([xshift=-\shift,yshift=\shift]current page.center) 
  rectangle   
  ([xshift=\shift,yshift=-\shift]current page.center);
\end{scope}
\end{tikzpicture}

\clearpage
\begin{tikzpicture}[remember picture,overlay]
\draw[green] (current page text area.north west) -- (current page text area.south east);
\draw[green] (current page text area.north east) -- (current page text area.south west);
\node[draw,circle,red] at (current page.center) {};
\node[draw,circle,green] at (current page text area.center) {};

\begin{scope}[color=gray,line width=1pt]
\foreach \shift in {1cm,2cm,3cm,4cm,5cm,6cm}
  \draw 
  ([xshift=-\shift,yshift=\shift]current page text area.center) 
  rectangle   
  ([xshift=\shift,yshift=-\shift]current page text area.center);
\end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

在对答案的评论中提出了一个新的要求:在中心画一条水平线;由于没有指定线的宽度,我显示了两种可能性:宽度等于框的宽度,宽度等于\paperwidth

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{tikzpagenodes}

\pagestyle{empty}
\def\shift{4cm}

\begin{document}

\begin{tikzpicture}[remember picture,overlay]
\node[draw,circle,green] at (current page text area.center) {};% just to visualize the center
\begin{scope}[color=gray,line width=1pt]
\draw 
  ([xshift=-\shift,yshift=\shift]current page text area.center) 
  rectangle   
  ([xshift=\shift,yshift=-\shift]current page text area.center);
\end{scope}
% A horizontal rule having the same width as the box
\draw[line width=8pt,orange] 
  ([xshift=-\shift]current page text area.center) -- 
  ([xshift=\shift]current page text area.center);
% A horizontal rule having the same width as the page
\draw[ultra thick,blue] 
  (current page.west|-current page text area.center) -- 
  (current page.east|-current page text area.center);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容