如何计算线剩余部分的宽度

如何计算线剩余部分的宽度

我希望使用titlesec类似图像创建章节标题

在此处输入图片描述

但是我在确定包含部分标签的蓝色框的剩余线的宽度时遇到了一些困难,我的方法是从灰色框的长度中减去,textwidth但它不起作用。

\documentclass[a4paper]{book}
\usepackage{lipsum}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}

\newlength{\myl}
\colorlet{mygray}{gray!90}
\colorlet{myblue}{blue!80}

\newcommand{\graybox}{\colorbox{mygray}{\strut \color{white}Section~\thesection}}

\settowidth{\myl}{\graybox}


\titleformat{\section}[hang]{\large\bfseries}%
{\graybox}{.5ex}{\colorbox{myblue}{\makebox[\dimexpr \linewidth-\myl-2\fboxsep-.5ex][l]%
{\strut \color{white}\large\bfseries #1}}}

\pagestyle{empty}

\begin{document}

在此处输入图片描述

答案1

计算结果与其他答案有些类似,但该解决方案还可以处理未编号的部分和长标题。

\documentclass[a4paper]{book}
\usepackage{lipsum}
\usepackage{titlesec}
\usepackage{xcolor}

\newsavebox{\sectionlabelbox}
\newlength{\sectionlabelwidth}
\colorlet{mygray}{gray!90}
\colorlet{myblue}{blue!80}

\newcommand{\sectionlabel}{%
  \sbox{\sectionlabelbox}{\colorbox{mygray}{\strut\color{white}Section~\thesection}}%
  \global\sectionlabelwidth=\wd\sectionlabelbox
  \usebox{\sectionlabelbox}%
}
\newcommand{\sectiontitle}[1]{%
  \colorbox{myblue}{%
    \parbox[t]{\dimexpr\columnwidth-\sectionlabelwidth-2\fboxsep-0.5ex}{
      \raggedright\strut\color{white}#1
    }%
  }%
}

\titleformat{\section}[hang]
  {\large\bfseries\global\sectionlabelwidth=-0.5ex }%
  {\sectionlabel}
  {.5ex}
  {\sectiontitle}

\begin{document}

\section{A test}

\setcounter{section}{9}

\section{Another test}

\section{Another test, but with a title that is so long it has to be
split across lines}

\section*{A further test}

\end{document}

在此处输入图片描述

这是一个修改,其中灰色框具有与蓝色框相同的垂直尺寸。

\documentclass[a4paper]{book}
\usepackage{lipsum}
\usepackage{titlesec}
\usepackage{xcolor}

\newsavebox{\sectiontitlebox}
\newlength{\sectionlabelwidth}
\colorlet{mygray}{gray!90}
\colorlet{myblue}{blue!80}

\newcommand{\sectiontitle}[1]{%
  \settowidth{\sectionlabelwidth}{%
    \colorbox{mygray}{\strut\color{white}Section~\thesection}%
  }%
  \sbox{\sectiontitlebox}{%
    \colorbox{myblue}{%
      \parbox[t]{\dimexpr\columnwidth-\sectionlabelwidth-2\fboxsep-0.5ex}{
        \raggedright\strut\color{white}#1
      }%
    }%
  }%
  \colorbox{mygray}{%
    \vrule height \dimexpr\ht\sectiontitlebox-\fboxsep\relax
           depth  \dimexpr\dp\sectiontitlebox-\fboxsep\relax
           width 0pt
    \color{white}Section~\thesection
  }%
  \hspace{0.5ex}%
  \usebox{\sectiontitlebox}%
}

\newcommand{\sectionstartitle}[1]{%
  \colorbox{myblue}{%
    \parbox[t]{\dimexpr\columnwidth-2\fboxsep}{
        \raggedright\strut\color{white}#1
    }%
  }%
}

\titleformat{name=\section}[hang]
  {\large\bfseries}
  {}
  {0pt}
  {\sectiontitle}
\titleformat{name=\section,numberless}[hang]
  {\large\bfseries}
  {}
  {0pt}
  {\sectionstartitle}

\begin{document}

\section{A test}

\setcounter{section}{9}

\section{Another test}

\section{Another test, but with a title that is so long it has to be
split across lines}

\section*{A further test}

\end{document}

在此处输入图片描述

答案2

基于有没有办法测量一行文本的剩余空间?

\documentclass[a4paper]{book}
\usepackage{lipsum}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}

\newlength{\myl}
\colorlet{mygray}{gray!90}
\colorlet{myblue}{blue!80}

\newcommand{\graybox}{\colorbox{mygray}{\strut \color{white}Section~\thesection}}

\settowidth{\myl}{\graybox}

\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\currentsidemargin}{%
    \ifodd\value{page}%
    \oddsidemargin%
    \else%
    \evensidemargin%
    \fi%
}

\newlength{\whatsleft}

\newcommand{\measureremainder}[1]{%
    \begin{tikzpicture}[overlay,remember picture]
    % Helper nodes
    \path (current page.north west) ++(\hoffset, -\voffset)
    node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\paperwidth, minimum height=\paperheight]
    (pagearea) {};


    \path (pagearea.north west) ++(1in+\currentsidemargin,-1in-\topmargin-\headheight-\headsep)
    node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\textwidth, minimum height=\textheight]
    (textarea) {};

    % Measure distance to right text border
    \path let \p0 = (0,0), \p1 = (textarea.east) in
    [/utils/exec={\pgfmathsetlength#1{\x1-\x0}\global#1=#1}];

    \end{tikzpicture}%
}

\titleformat{\section}[hang]{\large\bfseries}%
{\graybox}{.5ex}{\measureremainder{\whatsleft}\colorbox{myblue}{\makebox[\dimexpr\whatsleft-2\fboxsep][l]%
{\strut\color{white}\large\bfseries #1}}}

\pagestyle{empty}

\begin{document}

\section{blub}

\setcounter{section}{10}

\section{blub}

\lipsum

\end{document}

在此处输入图片描述

相关内容