问题

问题

问题

我如何计算标题后的距离,以便我可以将节点放置在标题规则所在的位置?

例子

我希望有一个栏从页面的右上角向下移动到标题规则(无论我对定义做了什么更改,它都应该完全对齐titlesec)。如果有人能帮助我将章节编号与章节标题对齐,我将不胜感激。

示例代码

\documentclass[10pt]{article}

\usepackage{fontspec}

% Title Styles
\usepackage[compact,explicit,noindentafter]{titlesec}
\usepackage{tocloft}
\usepackage{needspace}
\usepackage{tikz} % Absolute positioning, advanced vector graphics
\usepackage{tikzpagenodes} % Adds nodes around page boxes (e.g. body)
\usetikzlibrary{calc,positioning,decorations} % Extensions for tikz (increase compilation time)
\usepackage{anyfontsize}


\newcommand{\tikztitlenumber}[1]{%
\begin{tikzpicture}[remember picture, overlay,baseline]
    \node [inner sep=0pt, outer sep=0pt,anchor=base] (titlenumber) at (current page text area.north east) {#1};
    \path [fill=blue] (current page.north east) rectangle ($ (titlenumber.south east -| current page.east) + (-3mm,-\baselineskip) $);
\end{tikzpicture}}

% SECTION
\titleformat{\section}[hang]{\needspace{6cm}\color{blue}\Huge\bfseries}{\tikztitlenumber{\thesection}}{0pt}{#1}[\vspace{0mm}\titlerule]

\begin{document}
\section{Hello}

\end{document}

答案1

由于没有tikz专家给出解决方案,因此这里是非专家的尝试......

\documentclass[10pt]{article}

\usepackage{fontspec}

% Title Styles
\usepackage[compact,explicit,noindentafter]{titlesec}
\usepackage{tocloft}
\usepackage{needspace}
\usepackage{tikz} % Absolute positioning, advanced vector graphics
\usepackage{tikzpagenodes} % Adds nodes around page boxes (e.g. body)
\usetikzlibrary{calc,positioning,decorations} % Extensions for tikz (increase compilation time)
\usepackage{anyfontsize}
\usepackage{lipsum}

\newcommand{\tikztitlebar}{%
\begin{tikzpicture}[remember picture, overlay,baseline]
    \node (titlenumber) {};
    \path [fill=blue] (current page.north east) rectangle ($ (titlenumber.south east -| current page.east) + (-3mm,-0.4pt) $);
\end{tikzpicture}}

% SECTION
\titleformat{\section}[hang]
    {\needspace{6cm}\color{blue}\Huge\bfseries}
    {}
    {0pt}
    {#1\hfill\thesection\tikztitlebar}
    [\vspace{0mm}\titlerule]

\begin{document}
\lipsum[1]
\section{Hello}

\end{document}

输出

在此处输入图片描述

相关内容