![Tikz 圖形與頁邊距對齊](https://rvso.com/image/392314/Tikz%20%E5%9C%96%E5%BD%A2%E8%88%87%E9%A0%81%E9%82%8A%E8%B7%9D%E5%B0%8D%E9%BD%8A.png)
我正在使用樣式表中進行一些部分劃分tikz
。然而,即使我開始在它處繪製線條,它也(0,0)
與實際頁面內容邊框有偏移。我也設置了\parindent
,但仍然不完全對齊。如何強制該線恰好在邊界處開始(和結束)?
這是問題的一個最小範例:
\documentclass{article}
\usepackage[a4paper, left=14.111mm,right=14.111mm,top=14.111mm,bottom=14.111mm, showframe]{geometry}
\usepackage{tikz}
\newcommand{\linehalfhalf}{\begin{tikzpicture}[]%
\draw [line width=1.5mm ] (0,0) -- ({0.5\textwidth},0);
\draw [line width=0.5mm] ([yshift=+0.5mm]{0.5\textwidth},0) -- ([yshift=+0.5mm]{\textwidth},0) ;
\end{tikzpicture}}
\begin{document}
\setlength\parindent{0pt}
\linehalfhalf
\end{document}
答案1
感謝@JouleV 指出這個問題,我明白了:TikZ 中長度為 \textwidth 的線。
以下解決了該問題:
\newcommand{\linehalfhalf}{\begin{tikzpicture}[]%
\draw [line width=1.5mm, cap=rect] (0,0) -- ({0.5\linewidth},0);
\draw [line width=0.5mm, cap=rect] ([yshift=+0.5mm]{0.5\linewidth-\pgflinewidth},0) -- ([yshift=+0.5mm] \linewidth-0.5mm-\pgflinewidth,0) ;
\end{tikzpicture}}
答案2
一種解決方案是\clip
避免繪圖周圍有多餘的空間,但我認為更簡單的解決方案是在適當的位置填充一個細矩形來繪製一條線:
\documentclass{article}
\usepackage[a4paper, margin=14.111mm, paper height=5cm,showframe]{geometry}
\usepackage{tikz}
\newcommand{\linehalfhalf}{\begin{tikzpicture}[]%
\fill (0,0) rectangle (.5\textwidth,1.5mm);
\fill (0.5\textwidth,.5mm) rectangle (\textwidth,1mm) ;
\end{tikzpicture}}
\begin{document}%
\setlength\parindent{0pt}%
\linehalfhalf
\end{document}