3

I am creating a file, where the section headings should have box around the number. Also, some sections should not display this number even though the box should still be there. To solve this, I created two environments using titlesec, where in one environment the number has a different color than the box fill color, while in the other the colors match.

The problem I have is that both section numbers are displayed in the TOC. I looked up titlesec documentation and it mentioned something about using the \ifthesection condition to suppress numbers in TOC, but I cannot figure out how to do that.

I am attaching an example, with some dummy sections and the environments. I don't want the number for Exercises A (section 0.2) to be displayed. Can someone help me out on this?

Thank you,

Raman

Screenshot of current output

\documentclass[10.5pt,hidelinks]{book}
\usepackage[T1]{fontenc}
\usepackage{uarial}
\usepackage[ paper=a4paper,  layoutheight = 8.2in, layoutwidth  = 6in, layoutvoffset= 35mm, layouthoffset = 29mm, margin=0pt, includeheadfoot, showcrop=true]{geometry}
\usepackage{multicol}
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{helvet}
\usepackage{tikz}
\newcommand\sectionbar{%
\tikz[baseline,trim left=2.1cm,trim right=2.0cm] {
    %               \fill [black] (2.0cm,-0.3ex) rectangle (\textwidth+3.1cm,-0.1ex);
    \node [
    fill=lightgray,
    anchor= base east,
    rectangle,
    minimum height=3.5ex, minimum width=3.5ex] at (3cm,0) {
        %                   \color{white} \textbf{\arabic{\section}}
        \color{white}   \textbf{\thesection}
    };
    \fill [black] (\textwidth-13.15cm,-0.9ex) rectangle (\textwidth+2.1cm,-1.1ex);      
}%
}
 \titleformat{\section}{\large\bfseries}{\sectionbar}{1.5cm}{}

\newcommand\exercisebar{%
\tikz[baseline,trim left=2.1cm,trim right=2.0cm] {
    %               \fill [black] (2.0cm,-0.3ex) rectangle (\textwidth+3.1cm,-0.1ex);
    \node [
    fill=lightgray,
    anchor= base east,
    rectangle,
    minimum height=3.5ex, minimum width=3.5ex] at (3cm,0) {
        %                   \color{white} \textbf{\arabic{\section}}
        \color{lightgray}   \textbf{\thesection}
    };
    \fill [black] (\textwidth-13.15cm,-0.9ex) rectangle (\textwidth+2.1cm,-1.1ex);      
}%
}

\newenvironment{exercises}
  {\titleformat{\section}{\large\bfseries}{\exercisebar}{1.5cm}{}
%   \setcounter{secnumdepth}{0}%
%   \renewcommand\sectionmark[1]{}
    }
{%\setcounter{secnumdepth}{2}
   }
  \begin{document}
  \tableofcontents

  \section {LEARNING OBJECTIVES}
 \blindtext \\
  \lipsum[1]

 \begin{exercises}
 \section{Exercises A}
 \blindtext
 \lipsum[2-3]
 \end{exercises}


 \section{Exercises B}
 \blindtext
 \lipsum[4-5]


  \end{document}
1

I believe I found the most worst solution:

\newenvironment{exercises}{%
    \titleformat{\section}{\large\bfseries}{\exercisebar}{1.5cm}{}
    \addtocontents{toc}{%
         \string\let\string\oldnumberline\string\numberline%
         \string\renewcommand\string\numberline\string[1]\string\oldnumberline{\strut}}%
    }%  
 }{%
    \setcounter{secnumdepth}{2}
    \addtocontents{toc}{%
         \string\let\string\numberline\string\oldnumberline%
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.