如何在同一行的部分末尾添加符号/规则/ tikz 绘图,并保持默认间距

如何在同一行的部分末尾添加符号/规则/ tikz 绘图,并保持默认间距

我想在标题末尾添加符号/规则/ tikz 绘图以获得类似内容:

在此处输入图片描述

或者

在此处输入图片描述

我试图定义类似的东西:

\titleformat{\section}[runin]{\normalfont\Large\bfseries}{\thesection}{1em}{}[{\xrfill[1pt]{8pt}[secondaryColor]}\\]

(这{}似乎是必要的,否则将无法编译),但是该部分之后的间距和缩进是错误的:

在此处输入图片描述

正确的缩进和间距是:

在此处输入图片描述

获得适当间距的正确方法是什么\titlesec

梅威瑟:请注意,对于 tikz 绘图,我不知道这是否是最好的方法(我使用 3 张 tikz 图片,其中 2 张以 为remember picture间隔hfill,一张overlay负责绘图),但我找不到更好的方法来获取当前行的开始/结束位置。如果您知道,请告诉我。

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usepackage{lipsum}

\newcommand{\mytikzDrawing}{%
  % Ugly trick to find start/end of line... happy to hear better solutions.
  \begin{tikzpicture}[remember picture]
    \node[inner sep=0pt] (startPosition) {\phantom{X}};
  \end{tikzpicture}\hfill\begin{tikzpicture}[remember picture]
    \node[inner sep=0pt] (endPosition) {\phantom{X}};
  \end{tikzpicture}%
  \begin{tikzpicture}[remember picture,overlay]
    % \node[fill=blue!50,minimum width=\linewidth,minimum height=3mm] {AAA};
    \fill[left color=red!30, right color=yellow!30] (startPosition.north west) -- (endPosition.north east) -- (endPosition.south east) -- (startPosition.south west) -- cycle;
    \draw[decorate,decoration=snake,draw=blue!50] (startPosition.east) -- (endPosition.west);
  \end{tikzpicture}%
}

\usepackage{verbatim}
\usepackage{xhfill} % To draw color \hrulefill https://tex.stackexchange.com/a/155960/

\usepackage{xcolor}
\definecolor{secondaryColor}{RGB}{255,236,209}

\usepackage{titlesec}
%% Bad spacing after:
%\titleformat{\section}[runin]{\normalfont\Large\bfseries}{\thesection}{1em}{}[{\xrfill[1pt]{8pt}[secondaryColor]}\\]
% or with the tikz version:
% \titleformat{\section}[runin]{\normalfont\Large\bfseries}{\thesection}{1em}{}[\ \mytikzDrawing\\]

\begin{document}

Here is what I get when using nothing:

\section{Hello}

I'd prefer to get something like that (the problem being the spacing):\\

\noindent{\normalfont\Large\bfseries 2 \hspace{.3em} A simple version \xrfill[1pt]{8pt}[secondaryColor]}\\

Or something like that:

\noindent{\normalfont\Large\bfseries 3 \hspace{.3em} A complex version \mytikzDrawing{}}

\lipsum[1]

\end{document}

答案1

感谢 Werner 的评论指出这个问题explicit,我设法理解了该怎么做。这个想法是在包中加载选项,这会强制用户添加#1标题应该出现的位置,并将 tikz 代码放在前置文本中(与一起#1),而不是放在后置文本中。这样,形状runin就不需要了,间距直接是预期的:

\usepackage[explicit]{titlesec}
\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{#1\hrulefill}

有关tikz使用linegoal简化确定线的剩余部分的过程的完整示例(请参阅彩色线的注释代码):

在此处输入图片描述

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usepackage{linegoal} % \linegoal gives the remaining space on current line
\usepackage{tabularx}
\usepackage{lipsum}
\usepackage{tcolorbox}
\newlength{\remDim} % Dimension that remains after the section title
\newcommand{\mytikzDrawing}{%
  \setlength\remDim{\linegoal}% Store the remaining dimension to draw a node of the appropriate width
  \begin{tikzpicture}%
    \node[minimum width=\remDim,minimum height=7pt,inner sep=0pt,fill,left color=red!30, right color=yellow!30] (mainbox) {};
    \draw[decorate,decoration=snake,draw=blue!50] (mainbox.east) -- (mainbox.west);
  \end{tikzpicture}%
}

\usepackage{xhfill} % To draw color \hrulefill https://tex.stackexchange.com/a/155960/
\usepackage{xcolor}
\definecolor{secondaryColor}{RGB}{255,236,209}

\usepackage[explicit]{titlesec}
%% Rule version:
% \titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{#1\hrulefill}
%% Colored rule version:
% \titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{#1\xrfill[1pt]{8pt}[secondaryColor]}
%% Tikz version:
\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{#1 \mytikzDrawing}

\begin{document}

\section{A first section}

\lipsum[1]

\section{A second section}

\lipsum[1]

\end{document}

相关内容