0

I am trying to write a book in latex so I have the following chapter style in my latex code: enter image description here

How can this code be modified to get the following chapter style? enter image description here

\documentclass[twoside,a4paper,cleardoublepage=empty,14pt]{book}
                 

\begin{document}

\tableofcontents

\chapter{Analysis of Algorithms}    

  
\end{document}
2
  • We don't have anything you've \input, so it's hard to tell what you've done and what you need to do. Please trim this down to a minimal working example. And please show us your attempt at creating that chapter heading. This site doesn't work well with "Please do this for me" questions.
    – Teepeemm
    Commented Oct 19, 2023 at 14:31
  • @Teepeemm I try to use fancy packages to create such a style but it gives other styles.
    – tstt
    Commented Oct 19, 2023 at 15:07

1 Answer 1

2

Something like this?

\documentclass{book}
\usepackage{tikz}
\usepackage{titlesec}
\usepackage{lipsum}
\makeatletter
\newcommand*{\@chaptercolor}{red!50}
\newcommand*{\chaptercolor}[1]{%
    \renewcommand*{\@chaptercolor}{#1}%
}
\titleformat{\chapter}{\sffamily\bfseries\Large}{%
    \smash{%
        \begin{tikzpicture}[baseline=0]
            \useasboundingbox (-1,-1) rectangle (1,1);
            \draw[white, line width=4pt, fill=\@chaptercolor] (-1,-1) rectangle (1,1);
            \draw (-1,-1) rectangle (1,1);
            \node at (0,0) {\Huge\thechapter};
            \node at (0,1.3) {Chapter};
            \draw[overlay, line width=.2pt] (1.1,0) -- (\dimexpr\linewidth-1cm,0);
        \end{tikzpicture}%
    }%
}{0mm}{\newline\hspace*{2mm}}
\makeatother
\begin{document}
\chapter{Analysis of Algorithms}

\lipsum[1]

\chaptercolor{blue!50}
\chapter{Another chapter}

\lipsum[2]
\end{document}

You can change the color of the box to any color that is understood by TikZ with \chaptercolor{<color>}. For example, the above code also outputs the following.

The new color will stay the same until you change it again.

4
  • Is it possible to change the color of the orange box for a specific chapter?
    – tstt
    Commented Oct 20, 2023 at 17:29
  • @MR_ Yes, I edited my answer to make it easy!
    – Vincent
    Commented Oct 20, 2023 at 17:58
  • Could you look at this problem? tex.stackexchange.com/questions/699607/…
    – tstt
    Commented Oct 27, 2023 at 1:29
  • @MR_ I'm sorry I don't really have any experience with tasks or exsheets.
    – Vincent
    Commented Oct 27, 2023 at 15:16

You must log in to answer this question.

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