问题
为什么尽管添加了参数,TikZ 还是不记得我的节点的位置remember picture
?(此外,我该如何改进这个问题的标题?)
预期结果
蓝线应从节点的左下方绘制titletext
到节点的右下方titlenumber
。
实际结果
记错titlenumber
了,因此线画在页面的一半处。
编译器
xelatex Version 3.1415926-2.5-0.9999.3
代码
\documentclass{article}
\usepackage{fontspec}
\usepackage[compact,explicit,noindentafter]{titlesec}
\usepackage[rightlabels,dotinlabels]{titletoc}
\usepackage{needspace}
\usepackage{tikz} % Absolute positioning, advanced vector graphics
\usepackage{anyfontsize}
\newcommand{\tikzsectitle}[2]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
\node [text width=.9\textwidth] (titletext) {#1};
\end{tikzpicture}
}%
\newcommand{\tikzsecnumber}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
\node [font=\Huge\fontsize{60}{70}\selectfont,anchor=east] (titlenumber) at (titletext.center) {#1};
\end{tikzpicture}
}%
\newcommand{\tikztitleline}{%
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0pt,outer sep=0pt}]
\draw (titletext.south west |- titlenumber.south) -- (titlenumber.south east);
\end{tikzpicture}
}%
% TITLE FORMATTING
\titleformat{\section}[hang]{\needspace{6cm}\color{blue}\Huge\bfseries}{}{0pt}{\tikzsectitle{#1}\hfill\tikzsecnumber{\thesection}}[\tikztitleline]
\titlespacing{\section}{0pt}{40pt}{2.3ex plus .2ex}
\begin{document}
\section{Test}
\end{document}
输出
笔记
为了完整起见,我将指出我的代码中的一些问题。
- 蓝线的 yshift 不适用于这原因。
答案1
您的代码存在一些问题,因此我建议您进行以下修改:
\documentclass{article}
\usepackage{fontspec}
\usepackage[compact,explicit,noindentafter]{titlesec}
\usepackage[rightlabels,dotinlabels]{titletoc}
\usepackage{needspace}
\usepackage{showframe}
\usepackage{tikz} % Absolute positioning, advanced vector graphics
\usepackage{anyfontsize}
\usetikzlibrary{calc}
\newcommand{\tikzsectitle}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
\node [text width=.7\textwidth] (titletext) {#1};
\end{tikzpicture}
}%
\newcommand{\tikzsecnumber}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
\node [overlay,anchor=east,font=\Huge\fontsize{60}{70}\selectfont,text width=0.1\textwidth] (titlenumber) at ( $ (titletext.west) + (\textwidth,0) $ ){#1};
\end{tikzpicture}
}%
\newcommand{\tikztitleline}{%
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0pt,outer sep=0pt}]
\draw [yshift=-2ex] (titletext.south west |- titlenumber.south) -- (titlenumber.south east);
\end{tikzpicture}
}%
% TITLE FORMATTING
\titleformat{\section}[hang]{\needspace{6cm}\color{blue}\Huge\bfseries}{}{0pt}{\tikzsectitle{#1}\tikzsecnumber{\thesection}}[\tikztitleline]
\titlespacing{\section}{0pt}{40pt}{2.3ex plus .2ex}
\begin{document}
\section{Test}
some test text some test text some test text some test text some test text some test text some test text some test text some test text some test text
\end{document}
变化:
您最初声明了
\tikzsectitle
两个强制参数:\newcommand{\tikzsectitle}[2]{...
位只使用它一个
\titleformat
:\titleformat{\section}[hang]{\needspace{6cm}\color{blue}\Huge\bfseries}{}{0pt}{\tikzsectitle{#1}\tikzsecnumber{\thesection}}[\tikztitleline]
这显然产生了一些奇怪的结果,这些结果不是立即引人注意的,但确实存在(例如,如果你抑制了,那么
\hfill
就找不到该数字的节点)。修改后的版本
\tikzsecnumber
\newcommand{\tikzsecnumber}[1]{% \begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}] \node [overlay,anchor=east,font=\Huge\fontsize{60}{70}\selectfont,text width=0.1\textwidth] (titlenumber) at ( $ (titletext.west) + (\textwidth,0) $ ){#1}; \end{tikzpicture} }%
我添加了
overlay
选项text width
(这是可选的,但我不确定这里所需的设置),更改了锚点并将节点的位置从 更改为(titletext.center)
(( $ (titletext.west) + (\textwidth,0) $ )
添加calc
了库),所以现在数字将被刷新到右边距,而不依赖于标题的宽度。修改后的版本
\titleformat
:\titleformat{\section}[hang]{\needspace{6cm}\color{blue}\Huge\bfseries}{}{0pt}{\tikzsectitle{#1}\hfill\tikzsecnumber{\thesection}}[\tikztitleline]
原始代码中不需要这个
\hfill
,因为定位是使用节点坐标完成的。
结果(我添加了showframe
边距的视觉指导,并更改了标题的文本宽度以用于演示目的):
答案2
您插入节点的位置错误。将锚点更改at (titletext.center)
为at (titletext.east)
\newcommand{\tikzsecnumber}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
\node [font=\Huge\fontsize{60}{70}\selectfont,anchor=east] (titlenumber) at (titletext.east) {#1};
\end{tikzpicture}
}%