4

I want to add a box to a repeated section of a textbook, which provides question discussions. Is it possible to do it by adding "tex style setting" command(s) to the preamble? I am using the memoir class.

(I require it because I am generating the principal content in my LaTeX file, via Scrivener and Multimarkdown, rather than writing it manually.)

I used tex from here to generate some nice Section headings. What I would like to do is to apply similar styling to 2 sections at the end of the chapter—Summary and Discussions—but because these are not sections, as such, I want their headings without the section numbering.

This is the critical tex:

\newcommand\titlebar{%
\tikz[baseline,trim left=3.1cm,trim right=3cm] {
    \fill [black!10] (2.5cm,-1ex) rectangle (\textwidth+3.8cm,2.5ex);
    \node [
        fill=cyan!60!white,
        fill=black!90!white,
                    anchor= base east,
        rounded rectangle,
        minimum height=3.75ex] at (2.9cm,0) {
        \textbf{\arabic{chapter}.\thesection.}
        \color{white}\textbf{T\thesection}
    };
}%
}
\titleformat{\section}{\large}{\titlebar}{0.1cm}{}
\renewcommand*{\thesection}{\arabic{section}}

This applies a nice styling to my section headings, including the section number. Now I want to apply a similar styling to two sections, Summary and Discussion, which appear at the end of the chapter. But I want those two sections to NOT have section numbers.

  • Welcome to the TeX.SX network, meta.tex.stackexchange.com/questions/1436/welcome-to-tex-sx! Would it be possible for you to post an example of your efforts, if any? The fastest way is to use the \fbox command, but you are probably looking for something more stylish. Could you provide us details of your layout ideas? – Malipivo Mar 26 '14 at 9:28
  • This question is doing something similar to what you try to achive, I guess. The main difference is that it is about the ToC rather than the sections in the text. But it could be easily extended to your case. – Ruben Mar 26 '14 at 9:35
  • @Ruben — I can see this example has some similarities. I'm unclear which elements I might need to adapts to produce the selection of the particular elements that I want to style, but I'll experiment a bit and see if I can make something of it! – johnbrc Mar 26 '14 at 12:11
  • What about using a \section* and manually adding it to the TOC if desired? – cslstr Mar 26 '14 at 13:10
4

I would say it is the best to define a new macro for the crucial sections like

\newcommand{\dsection}[1]{\section*{\titlebar*#1}}

with a simplified (starred) version of \titlebar (see the complete code below).

\documentclass{memoir}
\usepackage{titlesec,letltxmacro}
\usepackage{lipsum}
\usepackage{tikz}\usetikzlibrary{shapes.misc}
\makeatletter
\newcommand\titlebar@@{%
\tikz[baseline,trim left=3.1cm,trim right=3cm] {
    \fill [cyan!25] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
}}
\newcommand\titlebar@{%
\tikz[baseline,trim left=3.1cm,trim right=3cm] {
    \fill [cyan!25] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=cyan!60!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.5ex] at (3cm,0) {
        \textbf{\arabic{chapter}.\thesection.}
    };
}}
\newcommand\titlebar{\@ifstar\titlebar@@\titlebar@}
\titleformat{\section}{\large}{\titlebar}{0.1cm}{}
\renewcommand*{\thesection}{\arabic{section}}

\LetLtxMacro{\LtxSection}{\section}
\newcommand{\dsection}[1]{\LtxSection*{\titlebar*#1}}
\renewcommand{\section}[2][]{%
  \def\secname{#2}
  \ifx\somename\secname
    \LtxSection*{\titlebar*#2}
  \else
    \LtxSection[#1]{#2}
  \fi}
\def\somename{Summary}
\makeatother

\begin{document}
\chapter{First Chapter}
\section{Section name}
\lipsum[2]

\dsection{Some other Chapter}
\lipsum[2]

\section{Summary}
\lipsum[2]
\end{document}

Note: As requested the \desction format will be applied automatically on every section named "Summary".

example

Addendum

To get the section heading in bold you simply need to change the line

\titleformat{\section}{\large}{\titlebar}{0.1cm}{}

to

\titleformat{\section}{\large\bfseries}{\titlebar}{0.1cm}{}
|improve this answer|||||
  • thanks; this is helpful and along the lines I was thinking. The critical command is: \newcommand{\dsection}[1]{\section*{\titlebar*#1}}, which is implemented in the document, by inserting \dsection command. I believe this will work if I insert this tex manually into my MultiMarkdown document, at the relevant section. As a matter of interest is there a way of selecting named sections WITHIN the preamble? I.e. essentially saying implement \dsection wherever the section is labelled {Discussion}, which would avoid the need for inserting latex into the MMD file. Thanks! – johnbrc Mar 26 '14 at 13:38
  • Be careful when using the term "labeled" in the LaTeX context. Originally it means that you put a \label{xyz} tag in the body, which you can refer to by calling \ref{xyz} and its 'cousins'. I Guess you want to mark a certain section to declare that it should behave like a "\dsection". Am I right? – Ruben Mar 26 '14 at 15:28
  • Thanks for the advice. Ideally, I want the \dsection behaviour to be adopted by each Summary and Discussion section, WITHOUT having to mark them with latex (I am composing in Scrivener, which generates a LaTex PDF). But I wonder if that may not be possible. – johnbrc Mar 26 '14 at 15:49
  • In using the macro, I've noticed that both section and \dsection headings are no longer bold text. Is there a simple way to amend the macro to restore it? – johnbrc Mar 26 '14 at 15:54
  • Yes. And Regarding you other comment: Without telling LaTeX what you want to do, it is impossible by definition, because LaTeX is doing all the work in this solution right now. – Ruben Mar 26 '14 at 15:56

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.