1

I want to format my chapter headings so they look something like this:

------ 1 --------------------------------- % horizontal rule here

Here is the title for chapter 1

------------------------------------------ % horizontal another rule

I can use the titlesec package to create most of this, but I can't get the first line I want, i.e. the chapter number surrounded by raised horizontal rules. I can almost get it right with \hrulefill using:

\hrulefill\thechapter \hrulefill\hrulefill

but the rule produced is at baseline height, and I cannot figure out how to raise the \hrulefill command. On the internet I stumbled on an alternative by using leaders:

\newcommand{\raisedrule}[2][0em]{\leavevmode\leaders\hbox{\rule[#1]{1pt}{#2}}\hfill\kern0pt}

but this also does not produce quite what I want. It successfully raises the line, but the result looks more like a series of concatenated small lines(of 1pt length, I guess), instead of a unified rule. I've tried to reduce the size of the individual units by setting it to something like \rule[#1]{0.1pt} but that seems to significantly slow down the rendering of my PDF viewer (in one instance it even crashed it). I'm not sure why.

I've also tried the xhfill package, but including two xrfill commands next to each seems to introduce a small gap between them.

Any ideas on how to achieve this, either by modifying \hfillrule or introducing some other commands ?

Thanks.

MWE:

\documentclass[11pt,twoside]{book}


% I've tried two ways of defining the rule... 
% I've tried this option:
\newcommand{\raisedrule}[2][0em]{\leavevmode\leaders\hbox{\rule[#1]{1pt}{#2}}\hfill\kern0pt}
\newcommand{\chaphrfill}{\raisedrule[10pt]{5pt}}

% And I've also tried this: 
\usepackage{xhfill}
\newcommand{\chaphrfill}{\xrfill[10pt]{5pt}}


\usepackage[explicit]{titlesec}
\titleformat{\chapter}[display]
{\bfseries\scshape}
{%
  \hspace*{0em}
{\chaphrfill~~\thechapter~~\chaphrfill\chaphrfill}
 }
 {0em}
 {\Large\raggedright#1}    


\begin{document}

\chapter{Chapter title....}
 text .....
\end{document}
1
  • 1
    Could you post the code of whatt you've tried?
    – Bernard
    Jul 24, 2019 at 21:37

2 Answers 2

2

You can use TiKz, like this.

\documentclass[11pt,twoside]{book}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\titleformat{\chapter}[display]
{\bfseries\scshape}
{%
 \hspace*{-0.5em}
{\tikz \draw [line width=2mm](0,0)--(2,0)node[fill=white, right,
shift=({0cm,1cm})]{\scalebox{10} \thechapter}--(\textwidth,0);}
 }
 {-1em}
{\Large\raggedright#1 
\hspace*{-0.5em}
{\tikz \draw [line width=2mm](0,0)--(\textwidth,0);}}    

\begin{document}

\chapter{Chapter title....}

\end{document}

enter image description here

2
  • Oh wow, it didn't even occur to me to consider tikz (probably because I have no experience with it). This might be what I need. Is there any way I can control the vertical position of the top line, however ? The font size of the number "\thechapter" that I'm using is significantly bigger, and I want to be able to specify exactly where it should meet with the line....
    – johnymm
    Jul 24, 2019 at 22:40
  • If I understand you right, I have edited my answer.
    – user31034
    Jul 25, 2019 at 8:48
0

I don't think you need a hammersledge like TikZ to crack this nut. Is it something like this you're after?

\documentclass[11pt,twoside]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[svgnames]{xcolor}
\colorlet{headingcolour}{Silver!50!Lavender}
\usepackage{linegoal}
\usepackage[explicit]{titlesec}
\titleformat{\chapter}[display]
{\bfseries\scshape}
{\color{headingcolour}\rule[1.5ex]{0.2\textwidth}{4.5pt}~~\scalebox{4}{\thechapter}~~\rule[1.5ex]{\linegoal}{4.5pt}}
 {2ex}
 {\LARGE\filright#1}[\vskip1.5ex\leavevmode{\color{headingcolour}\titlerule[4.5pt]}]
 \titlespacing*{\chapter}{0pt}{*1}{*25}
 \usepackage{lipsum}

\begin{document}

\chapter{Chapter title....}

\lipsum[11]

\end{document}} 

enter image description here

3
  • Something like it, but there are a couple of issues with this solution. I don't need the number centered, but about 1/3 or 1/4 of the way to the left, and I could not achieve that with \xrfill. Second, the color on the rules is not uniform. It looks more like individual dashes stringed together.
    – johnymm
    Jul 25, 2019 at 1:42
  • I've posted a variant, no more using xhfill, but only the standard \rule, and titlerule. I'm using the linegoal package, which measures the distance from the end of the current line, but requires two compilations.
    – Bernard
    Jul 25, 2019 at 2:07
  • This works. Thank you.
    – johnymm
    Jul 25, 2019 at 2:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .