2

I'm new here and almost newbie to latex. Sorry for my English.

Environment: Miktex 4.1, TexnicCenter 2.02(64bit), mode: LaTeX=>PDF

I searched a lot. At first I searched for method to put chapters into a frame... ... and found this:

background color in titlesec

It wasn't exactly what I've been looking for but decided to work with that.

Additional information: I have 4 chapters.

First attempt:

...

\titleformat{\chapter}[display]
{\sffamily\bfseries}%
{}%
{}%
{\begin{adjustwidth}{-0.9in}{-0.8in}%
\tcbset{standard jigsaw,
    colback=ChapterBackColor,
    colframe=ChapterFrameColor,
    opacityback=0.1,
    arc=10pt,
    sharp corners=east,
    width=\linewidth-0.5in}%
\begin{tcolorbox}[rightrule=0mm,leftrule=3mm,toprule=1mm,bottomrule=0.5mm,flush right,halign=flush right,enlarge top by=-2.5cm,enlarge bottom by=1cm]%
\Large{}CHAPTER\huge{} \Roman{chapter}\strut\\[0.3ex] #1\end{tcolorbox}\end{adjustwidth}}

...


E D I T

Ok. Since my project contained tones of garbage, I needed to prepare ready-to-compile small samples for you. And this way I located error having vertical mode in message. There is even nothing to talk about.

But there are still errors and believe I'm doing something wrong using titleformat. I have four chapters and four errors:

! Missing number, treated as zero

and four errors:

! Illegal unit of measure (pt inserted)

Code to compile: ...

\documentclass[a4paper,11pt,oneside]{book}
\usepackage[top=1in,bottom=1in,left=1.2in,right=0.8in,headsep=10pt,a4paper]{geometry}
\usepackage{changepage}
\usepackage{polski}
\usepackage[utf8]{inputenc}
\usepackage{avant}
\usepackage{mathptmx}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\definecolor{ChapterFrameColor}{RGB}{222, 66, 44}
\definecolor{ChapterBackColor}{RGB}{255, 222, 111}
\definecolor{ChapterForeColor}{RGB}{ 55, 16, 11}
\definecolor{ListingFrameColor}{RGB}{200, 80, 50}
\definecolor{ListingBackColor}{RGB}{231, 227, 201}
\definecolor{ListingNumColor}{RGB}{251, 249, 241}
\usepackage{tcolorbox}
\usepackage{array}
\usepackage[colorlinks=true,linkcolor=blue,urlcolor=black,bookmarksopen=true]{hyperref}
\usepackage[
    open,
    openlevel=2,
    atend
]{bookmark}[2011/12/02]
\usepackage{tikz}
%
%
%
\usepackage[explicit]{titlesec}
%
\titleformat{\chapter}[display]
{\sffamily\bfseries}%
{}%
{}%
{%
%
\begin{adjustwidth}{-0.9in}{-0.8in}%
\tcbset{standard jigsaw,
    colback=ChapterBackColor,
    colframe=ChapterFrameColor,
    opacityback=0.1,
    arc=10pt,
    sharp corners=east,
    width=\linewidth-0.5in}%
%
\begin{tcolorbox}[rightrule=0mm,leftrule=3mm,toprule=1mm,bottomrule=0.5mm,flush right,halign=flush right,enlarge top by=-2.5cm,enlarge bottom by=1cm]%
\Large{}CHAPTER\huge{} \Roman{chapter}\strut\\[0.3ex]#1\end{tcolorbox}\end{adjustwidth}%
%
}
%
%
%
\usepackage{lipsum}
\tcbuselibrary{minted,skins}
\newtcblisting{mynumcpp}[1][]{listing engine=minted,
minted style=vs,
minted language=cpp,
minted options={fontsize=\small,breaklines,linenos,autogobble,numbersep=3mm,#1},
colback=ListingBackColor,colframe=ListingFrameColor,listing only,
left=7mm,enhanced,
overlay={\begin{tcbclipinterior}\fill[ListingNumColor] (frame.south west)
rectangle ([xshift=7mm]frame.north west);\end{tcbclipinterior}}}
%
\newtcblisting{mycpp}{listing engine=minted,
minted style=vs,
minted language=cpp,
minted options={fontsize=\small,breaklines,autogobble},
colback=ListingBackColor,colframe=ListingFrameColor,listing only,
enhanced}
%
\frenchspacing
\pagestyle{headings}

\begin{document}
\part{Part One}
\chapter{Some chapter}
\section{Section name A}
\lipsum[1-2]
\begin{mycpp}
int main() {
    printf("hello, world");
    return 0;
}
\end{mycpp}
\lipsum[3-4]
\chapter{Some other chapter}
\section{Section name B}
\lipsum[5-6]
\chapter{Chapter name}
\section{Section name C}
\lipsum[7-8]
\part{Part Two}
\chapter{4th chapter}
\section{Section name D}
\lipsum[5-6]

\end{document}

...

I can remove fragments with minted - if that would be a problem.

I don't know whether I should change a title of this topic or leave it as it is now.


E D I T - 2

Solution : Argument <sep> must be given.

5
  • Are you loading titlesec with the explicit option?
    – egreg
    Mar 12, 2021 at 7:48
  • Welcome to TeX SX! Could you please post a compilable,from \documentclass to \end{document}, loading the relevant packages? Also, please explain a bit which result you expect.
    – Bernard
    Mar 12, 2021 at 10:35
  • @jsbibra: thank you for edition. @egreg: yes. @Bernard: code pasted. Expected result is whole chapter title, so: chaptertitlename, chapter-number and chapter-caption inside tcolorbox. Mar 12, 2021 at 20:34
  • 1
    The third mandatory argument to \titleformat must contain a length and cannot be left empty.
    – egreg
    Mar 13, 2021 at 18:42

1 Answer 1

2

The third mandatory argument to \titleformat has to contain a length, even if you need 0pt.

Here's a reorganized version of your preamble, with clear separation of package loading and settings.

In particular, I advise to use babel rather than polski (which does a bit of strange things) and newtx instead of mathptmx.

I load titlesec without explicit, which allows to prepare code suitable for both numbered and unnumbered chapters with no code duplication. The idea is that the chapter title is passed braced to the code of the last mandatory argument, so it will be treated as a trailing argument.

Using one line per option is, in my opinion, better in order to see more clearly what you have activated and to experiment with inactivating or changing a few of them.

\documentclass[a4paper,11pt,oneside]{book}
\usepackage[top=1in,bottom=1in,left=1.2in,right=0.8in,headsep=10pt,a4paper]{geometry}
\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc}
%\usepackage{polski}
\usepackage[polish]{babel}

\usepackage{changepage}
\usepackage{avant}
%\usepackage{mathptmx}
\usepackage{newtxtext,newtxmath}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{minted,skins}
\usepackage{titlesec}
\usepackage{array}
\usepackage{lipsum}

\usepackage[
  colorlinks=true,
  linkcolor=blue,
  urlcolor=black,
  bookmarksopen=true
]{hyperref}
\usepackage[
    open,
    openlevel=2,
    atend
]{bookmark}[2011/12/02]

\definecolor{ChapterFrameColor}{RGB}{222, 66, 44}
\definecolor{ChapterBackColor}{RGB}{255, 222, 111}
\definecolor{ChapterForeColor}{RGB}{ 55, 16, 11}
\definecolor{ListingFrameColor}{RGB}{200, 80, 50}
\definecolor{ListingBackColor}{RGB}{231, 227, 201}
\definecolor{ListingNumColor}{RGB}{251, 249, 241}

\titleformat{name=\chapter}[display]
  {\sffamily\bfseries}
  {}
  {0pt}%
  {\bombadilchapter{1}}
\titleformat{name=\chapter,numberless}[display]
  {\sffamily\bfseries}
  {}
  {0pt}%
  {\bombadilchapter{0}}

\newcommand{\bombadilchapter}[2]{%
  \begin{adjustwidth}{-0.9in}{-0.8in}
  \begin{tcolorbox}[
    standard jigsaw,
    colback=ChapterBackColor,
    colframe=ChapterFrameColor,
    opacityback=0.1,
    arc=10pt,
    sharp corners=east,
    width=\linewidth-0.5in,
    rightrule=0mm,
    leftrule=3mm,
    toprule=1mm,
    bottomrule=0.5mm,
    flush right,
    halign=flush right,
    enlarge top by=-2.5cm,
    enlarge bottom by=1cm
  ]
  \ifnum#1=0
    \huge\strut\\[0.3ex]
  \else
    \Large \MakeUppercase{\chaptertitlename} \huge\Roman{chapter}\strut\\[0.3ex]
  \fi
  #2%
  \end{tcolorbox}
  \end{adjustwidth}
}


\newtcblisting{mynumcpp}[1][]{
  listing engine=minted,
  minted style=vs,
  minted language=cpp,
  minted options={
    fontsize=\small,
    breaklines,
    linenos,
    autogobble,
    numbersep=3mm,
    #1,
  },
  colback=ListingBackColor,
  colframe=ListingFrameColor,
  listing only,
  left=7mm,
  enhanced,
  overlay={%
    \begin{tcbclipinterior}
      \fill[ListingNumColor] (frame.south west) rectangle ([xshift=7mm]frame.north west);
    \end{tcbclipinterior}%
  },
}

\newtcblisting{mycpp}{
  listing engine=minted,
  minted style=vs,
  minted language=cpp,
  minted options={
    fontsize=\small,
    breaklines,
    autogobble
  },
  colback=ListingBackColor,
  colframe=ListingFrameColor,
  listing only,
  enhanced,
}

\frenchspacing
\pagestyle{headings}

\begin{document}
\part{Part One}

\chapter*{Introduction}

\lipsum[1-2]

\chapter{Some chapter}
\section{Section name A}
\lipsum[1-2]
\begin{mycpp}
int main() {
    printf("hello, world");
    return 0;
}
\end{mycpp}
\lipsum[3-4]
\chapter{Some other chapter}
\section{Section name B}
\lipsum[5-6]
\chapter{Chapter name}
\section{Section name C}
\lipsum[7-8]
\part{Part Two}
\chapter{4th chapter}
\section{Section name D}
\lipsum[5-6]

\end{document}

enter image description here

1
  • I appreciate your help and effort but i will stick to my solution because I understand it. You defined command bombadilchapter, called with one argument only and yet, you're using argument #2 inside. I don't know how it works. I choose package polski because it was updated in 2016 and my sources are using this. But I've never used polski package before. What i don't know is why you removed inputenc. Normally I don't mix dependencies and defs/settings ... but this is a draft only. Thank you. Mar 14, 2021 at 18:45

You must log in to answer this question.

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