我正在使用 tikz 为我的报告创建自定义标题页。我想绘制水平线,从页面主体的一个边缘延伸到另一个边缘,标题位于中间。当我只绘制线条时,它们确实从页面边缘开始。添加文本会将它们向右移动(看起来与文本中心对齐)。我怎样才能让所有这些对象从边缘开始?谢谢。
\begin{tikzpicture}
\draw [line width = 0.3mm, draw = black] (0in,0in) -- (4in,0in);
\node at (0,-0.75in) {\Huge\bfseries\sffamily\selectfont Title};
\draw [line width = 0.3mm, draw = black] (0,-1.5in) -- (4in,-1.5in);
\end{tikzpicture}
答案1
附加的文本节点会改变 TikZ 图片的尺寸。它变得更宽,因此线条似乎向右移动,因为图片从左侧开始,但它仍然需要放置在文本区域中。
可能的解决方案:
用于
overlay
节点,因此其尺寸不会影响 TikZ 图片边界框:\node[overlay] at (0,-0.75in) {\Huge\bfseries\sffamily\selectfont Title};
绝对定位 TikZ 图片,您可以使用
current page
节点。快速示例 - 编译两次:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[overlay, remember picture]
\draw [line width = 0.3mm, draw = black]
($(current page.north west)+( 1in,-1in)$)
-- ($(current page.north east)+(-3in,-1in)$);
\node [anchor=west, font=\Huge\bfseries\sffamily]
at ($(current page.north west)+(1in,-2in)$) {Title};
\draw [line width = 0.3mm, draw = black]
($(current page.north west)+( 1in,-3in)$)
-- ($(current page.north east)+(-3in,-3in)$);
\end{tikzpicture}
\end{document}
答案2
当您使用像 Tikz 这样的与文本交互的图形时,您需要包含该overlay
选项。
如果不添加此选项,则任何东西都会影响 Tikz 图形的位置。即使添加\noindent
也会产生变化。
输出
代码
\documentclass{article}
\usepackage{tikz}
\begin{document}
%Without overlay
\begin{tikzpicture}
\draw [line width = 0.3mm, draw = black] (0in,0in) -- (4in,0in);
\node at (0,-0.75in) {\Huge\bfseries\sffamily\selectfont Title};
\draw [line width = 0.3mm, draw = black] (0,-1.5in) -- (4in,-1.5in);
\end{tikzpicture}
\vspace*{3cm}
% with overlay
\begin{tikzpicture}[overlay]%, remember picture] <-- not necessary in this case.
\draw [line width = 0.3mm, draw = black] (0in,0in) -- (4in,0in);
\node at (0,-0.75in) {\Huge\bfseries\sffamily\selectfont Title};
\draw [line width = 0.3mm, draw = black] (0,-1.5in) -- (4in,-1.5in);
\end{tikzpicture}
\end{document}
答案3
你是对的,TikZ 中的文本节点位于中间,因此,如果要将标题与两条线对齐,则必须将坐标更改x
为这些线长度的一半:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [line width = 0.3mm, draw = black] (0in,0in) -- (4in,0in);
\node at (2in,-0.75in) {\Huge\bfseries\sffamily\selectfont Title};
\draw [line width = 0.3mm, draw = black] (0in,-1.5in) -- (4in,-1.5in);
\end{tikzpicture}
\end{document}
如果您想让标题更宽,保持元素居中,您可以使用以下代码:
\documentclass{article}
\usepackage{tikz}
%\usepackage[margin=10mm]{geometry}
\begin{document}
\begin{center}
\begin{tikzpicture}
\newlength{\titleLineWidth}
% set the title width to 90% of text width
\setlength{\titleLineWidth}{.9\textwidth}
\newlength{\titlePosition}
\setlength{\titlePosition}{\titleLineWidth}
% put the title just at the middle
\divide\titlePosition by 2
\draw [line width = 0.3mm, draw = black] (0in,0in) -- (\titleLineWidth,0in);
\node at (.5\titleLineWidth,-0.75in) {\Huge\bfseries\sffamily\selectfont Title};
\draw [line width = 0.3mm, draw = black] (0in,-1.5in) -- (\titleLineWidth,-1.5in);
\end{tikzpicture}
\end{center}
\end{document}
删除包的注释geometry
以查看更广泛的文档,从而查看更广泛的标题。