我想创建一个文档,其中章节标题是tikz
节点,并且我想用从每页右上角开始的路径连接每页中的节点。这可能吗?
Gonzalo 编辑(添加海峡对我的第一个回答的评论中):
我最初想要的是一条线,连接页面的右上角和第一节的右侧,然后将第一节的左侧连接到第二节的左侧,等等。所以它会像corner-|section 1 -|-section 2
(等等)一样,有点像蛇。我可以手动完成tikz
,但我想在每一页上自动完成。
答案1
如果我理解正确的话,你可以使用TikZ
以及titlesec
完成这个工作的包;大致如下:
\documentclass{article}
\usepackage{titlesec}
\usepackage{tikz}
\usepackage{lipsum}
\titleformat{\section}
{\normalfont\Large\bfseries}
{\tikz[remember picture,overlay,baseline=-6pt]
\node[fill=cyan!60!black,minimum width=1.5em,align=center] (a) {\thesection};}
{1em}
{\tikz[remember picture,overlay]
{\draw[cyan!60!black,ultra thick]
(a.west) -- +(-10pt,0pt) |- ([xshift=-20pt,yshift=-20pt]current page.north east);
\node[fill=cyan!60!black,minimum width=1em,minimum height=1em]
at ([xshift=-20pt,yshift=-20pt]current page.north east) {};
}%
}
\begin{document}
\section{Test Section One}
\lipsum[1-4]
\section{Test Section Two}
\lipsum[1-4]
\section{Test Section Three}
\lipsum[4]
\end{document}
在评论中,海峡已经解释了他/她想要实现的设计;其想法是得到一个蛇形图案,由水平和垂直的直线组成,曲折地穿过文档的各个部分。这需要做更多的工作,但仍然是可能的;代码需要运行三到四次才能稳定下来,对于在部分开始附近出现分页符的情况,还有一些工作要做:
\documentclass{article}
\usepackage{tikzpagenodes}
\usepackage{titlesec}
\usepackage{refcount}
\usepackage{xifthen}
\usepackage{atbegshi}
\usepackage{etoolbox}
\usepackage{lipsum}
\newcommand\pos{}
\newcounter{seccount}
\newcounter{linepages}
\newlength\linesep
\newlength\linesepaux
\newlength\mylinewidth
\setlength\linesep{40pt}
\colorlet{mycolor}{cyan!60!black}
\setlength\mylinewidth{6pt}
\setlength\linesepaux{\linesep}
\newcommand\tikzmark[1]{%
\tikz[remember picture,overlay] \coordinate (#1) {};}
\titleformat{\section}
{\normalfont\Large\bfseries}
{\thesection}
{1em}
{\begin{tikzpicture}[remember picture,overlay]
\draw[line width=\mylinewidth,mycolor] ([xshift=\the\dimexpr\linesepaux+0.5\mylinewidth\relax]current page text area.east|-s-\the\numexpr\thesection-1\relax) --
([xshift=\the\dimexpr-\linesepaux-0.5\mylinewidth\relax]current page text area.west|-s-\the\numexpr\thesection-1\relax);
\end{tikzpicture}%
}
\newcommand\StartSecLine{%
\stepcounter{seccount}
\ifthenelse{\isodd{\theseccount}}
{\global\renewcommand\pos{current page text area.north west}%
\global\setlength\linesep{\linesepaux}}
{\global\renewcommand\pos{current page text area.north east}%
\global\setlength\linesep{-\linesepaux}}
\tikzmark{e-\theseccount}\label{e-\theseccount}%
\ifnum
\getpagerefnumber{e-\theseccount}=\getpagerefnumber{s-\theseccount}\relax
\else
\begin{tikzpicture}[overlay, remember picture]
\draw [line width=\mylinewidth,mycolor]
([xshift=-\linesep]\pos|-e-\theseccount) -- ([xshift=-\linesep]\pos|-current page text area.south);
\end{tikzpicture}
\setcounter{linepages}{\numexpr\getpagerefnumber{s-\theseccount}-\getpagerefnumber{e-\theseccount}}
\ifnum\value{linepages}>1
\AtBeginShipoutNext{\tikzlinepage}%
\fi%
\fi%
}
\newcommand\EndSecLine{%
\tikzmark{s-\theseccount}\label{s-\theseccount}
\ifnum
\getpagerefnumber{s-\theseccount}=\getpagerefnumber{e-\theseccount}\relax
\begin{tikzpicture}[overlay, remember picture]
\draw [line width=\mylinewidth,mycolor]
([xshift=-\linesep]\pos|-e-\theseccount) -- ([xshift=-\linesep]\pos|-s-\theseccount);
\end{tikzpicture}%
\else
\begin{tikzpicture}[overlay, remember picture]
\draw [line width=\mylinewidth,mycolor]
([xshift=-\linesep]\pos) -- ([xshift=-\linesep]\pos|-s-\theseccount);
\end{tikzpicture}%
\fi
}
\newcommand\Initrules{%
\begin{tikzpicture}[remember picture,overlay]
\draw[line width=\mylinewidth,mycolor]
([xshift=\the\dimexpr\linesep\relax]current page text area.east|-e-1) --
([xshift=\linesep,yshift=-20pt]current page text area.east|-current page.north);
\draw[line width=\mylinewidth,mycolor]
([xshift=\the\dimexpr\linesep+0.5\mylinewidth\relax]current page text area.east|-e-1) --
([xshift=\the\dimexpr-\linesep-0.5\mylinewidth\relax]current page text area.west|-e-1);
\end{tikzpicture}%
}
\newcommand\tikzlinepage{%
\begin{tikzpicture}[overlay, remember picture]
\draw [line width=\mylinewidth,mycolor]
([xshift=-\linesep]\pos) --
([xshift=-\linesep]\pos|-current page text area.south west);
\end{tikzpicture}
\addtocounter{linepages}{-1}%
\ifnum\value{linepages}>1
\AtBeginShipoutNext{\tikzlinepage}%
\fi%
}
\AtBeginDocument{%
\noindent\tikzmark{s-0}
\StartSecLine
\Initrules}
\AtEndDocument{%
\EndSecLine}
\newcommand\StartMarks{\pretocmd{\section}{\EndSecLine\StartSecLine}{}{}}
\begin{document}
\section{Test Section One}
\lipsum[1-2]
\StartMarks
\section{Test Section Two}
\lipsum[1-8]
\section{Test Section Three}
\lipsum[4]
\section{Test Section Four}
\lipsum[1]
\section{Test Section Five}
\lipsum[4-5]
\section{Test Section Six}
\lipsum[4-6]
\end{document}
mycolor
通过更改、\mylinewidth
和的值\linesep
,可以轻松定制规则和文本之间的颜色、宽度和分离。