Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need chapter and section headings without numbers and with any leading spaces ignored (the document is generated by a program - dblatex). Here is an example:

\documentclass[12pt]{memoir}

\usepackage{lipsum}

\setcounter{chapter}{-1}
\setcounter{section}{-1}

\renewcommand{\chaptername}{}

\renewcommand{\thechapter}{}
\renewcommand{\thesection}{}

\begin{document}
\chapter{ mychapter }
\section{ mysection }
\lipsum
\end{document}

Unfortunately while chapter headings come out perfectly, section headings are indented, I assume, by

  1. gap between missing section number and the heading text (\quad?), and
  2. the leading space

While I can try to "remove" the first part of the indent by \setsechook{\hangsecnum}, the leading spaces have to be dealt with by manual editing. How can I achieve the same behavior as in chapter headings (no indent, leading space ignored)?

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

First of all, if you do not want numbers, then why don't you just ask memoir not to print them? \setsecnumdepth{none}

Second I agree, remove the leading space in your titles, but memoir can be recoded to ignore it

\makeatletter
\def\M@sect#1#2#3#4#5#6[#7][#8]#9{%
  \ifheadnameref\M@gettitle{#8}\else\M@gettitle{#7}\fi%
  \ifnum #2>\c@secnumdepth%
    \let\@svsec\@empty%
    \memsecinfo{#1}{}{#7}{#8}{#9}%
  \else%
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
    \memsecinfo{#1}{\@nameuse{the#1}}{#7}{#8}{#9}%
  \fi%
  \@tempskipa #5\relax%
  \ifdim \@tempskipa>\z@%
    \begingroup%
      #6{%
      \@hangfrom{\hskip #3\relax\@svsec}%
        \interlinepenalty\@M\ignorespaces #9\@@par}%
    \endgroup%
    \csname #1mark\endcsname{#8}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else%
        \protect\numberline{\csname the#1\endcsname}%
      \fi%
      #7}%
  \else%
    \def\@svsechd{%
      #6{\hskip #3\relax%
     \@svsec #9}%
     \csname #1mark\endcsname{#8}%
     \addcontentsline{toc}{#1}{%
       \ifnum #2>\c@secnumdepth \else%
        \protect\numberline{\csname the#1\endcsname}%
       \fi%
       #7}}%
  \fi%
  \@xsect{#5}}
\makeatother
share|improve this answer
    
Thanks, it works perfectly now! I wonder if there is a reason why chapter's vs. section's headings are processed differently with respect to treating leading spaces. –  Alex Vorobiev Sep 9 '11 at 17:22
    
very different constructions, as the section are usually much simpler than chapter titles. –  daleif Sep 11 '11 at 15:38
add comment

memoir provides the \setsecnumformat command for setting section numbering. This includes the space/gap between the section number and title. Add

\setsecnumformat{}

to your document preamble. Also remove the spaces in your section title:

\section{mysection}

Alternatively, if this forms part of an automated output, you can patch the \M@sect macro to automatically remove the spaces in front of the section title. Do this by adding the following in your document preamble:

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\patchcmd{\M@sect}{\@M #9}{\@M \ignorespaces #9}{}{}
\makeatother

See chapter 6 Document divisions (specifically p 107) of the memoir documentation for more information on section formatting.


Edit: In conversation you mentioned that you actually also use the hyperref package in your actual document. And, even though the above solved the problem without hyperref, it is insufficient for your broader needs. Here's how to fix the problem introduced by hyperref, and perhaps some investigative analysis on how one can go about solving such a problem:

The \patchcmd{<command>}{<search>}{<replace>}{<success>}{<failure>} would search-and-replace <search> with <replace> in <command>. For the sake of completeness, you could use \typeout{success} for <success> and \typeout{failure} for <failure> so that you will either see the word success or failure in <jobname>.log after you compile. This will give you an indication of whether or not the patch was successful. The reason for this is that sometimes a patch can do nothing because you patched the wrong part of the command (due to similarities - \patchcmd only replaces the first occurrence), or it could do nothing because the command is not patchable.

I modified the preamble to the following:

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter \show\M@sect \makeatother % \M@sect as defined in memoir
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\makeatletter \show\M@sect \makeatother % \M@sect as redefined by hyperref

The output in the <jobname>.log shows the meaning of \M@sect after each issue of \show\M@sect. Here's \M@sect as defined by memoir (of course, since this forms part of the <jobname>.log, the spacing/alignment is removed):

  > \M@sect=macro:
  #1#2#3#4#5#6[#7][#8]#9->\ifheadnameref \M@gettitle {#8}\else \M@gettitle {#7}\f
  i \ifnum #2>\c@secnumdepth \let \@svsec \@empty \memsecinfo {#1}{}{#7}{#8}{#9}\
  else \refstepcounter {#1}\protected@edef \@svsec {\@seccntformat {#1}\relax }\m
  emsecinfo {#1}{\@nameuse {the#1}}{#7}{#8}{#9}\fi \@tempskipa #5\relax \ifdim \@
  tempskipa >\z@ \begingroup #6{\@hangfrom {\hskip #3\relax \@svsec }\interlinepe
* nalty \@M #9\@@par }\endgroup \csname #1mark\endcsname {#8}\addcontentsline {to
  c}{#1}{\ifnum #2>\c@secnumdepth \else \protect \numberline {\csname the#1\endcs
  name }\fi #7}\else \def \@svsechd {#6{\hskip #3\relax \@svsec #9}\csname #1mark
  \endcsname {#8}\addcontentsline {toc}{#1}{\ifnum #2>\c@secnumdepth \else \prote
  ct \numberline {\csname the#1\endcsname }\fi #7}}\fi \@xsect {#5}.

The previous \patchcmd corrected the line marked with *. This is the meaning of \M@sect after hyperref was introduced:

  > \M@sect=macro:
  #1#2#3#4#5#6[#7][#8]#9->\ifnum #2>\c@secnumdepth \expandafter \@firstoftwo \els
  e \expandafter \@secondoftwo \fi {\Hy@MakeCurrentHrefAuto {section*}\setlength 
  {\Hy@SectionHShift }{#3}\begingroup \toks@ {\H@old@sectm@m {#1}{#2}{#3}{#4}{#5}
  {#6}[{#7}][{#8}]}\toks \tw@ \expandafter {\expandafter \Hy@SectionAnchorHref \e
  xpandafter {\@currentHref }#9}\edef \x {\endgroup \the \toks@ {\the \toks \tw@ 
* }}\x }{\H@old@sectm@m {#1}{#2}{#3}{#4}{#5}{#6}[{#7}][{#8}]{#9}}.

Since there's no more \@M #9 after hyperref's redefinition, the patch doesn't find a match and therefore does nothing. We need to insert \ignorespaces before #9 in the above hyperref redefinition of \M@sect - again marked with *. Therefore, we issue

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\makeatletter
\patchcmd{\M@sect}{{#9}}{{\ignorespaces#9}}{}{}%
\makeatother

to supply {\ignorespaces#9} (instead of {#9}) to the new \H@old@sectm@m macro. As such the patch works, and removed the preceding spaces in front of the section title.

share|improve this answer
    
Thanks. As I mentioned the documents are generated by dblatex out of files in docbook format. In those source files there are no spaces surrounding the sections/chapters titles. I can put whatever I want into preambula of the generated files but adjusting actual titles would involve figuring out how dblatex processes the corresponding docbook tags in its xslt code. –  Alex Vorobiev Sep 9 '11 at 17:30
    
@AlexV: You could also patch the code for \M@sect. I've updated my answer to include this patch. –  Werner Sep 9 '11 at 17:45
    
Thanks for the etoolbox package! While your patch worked with the sample document, plugging it into (huge) preambula of my actual files did not have any effect. (And I saw the "search pattern not found in replacement text" debug message when I tried \tracingpatches from etoolbox) The culprit turned out to be hyperref package which I neglected to include in the example because I stopped adding stuff from the preambula when I thought I reproduced the problem. Is there any easy way to find out how hyperref changes definition of \M@sect other than studying its sources? –  Alex Vorobiev Sep 11 '11 at 3:13
    
@AlexV: I've updated my answer to correct for the introduction of hyperref. I also included some discussion behind my solution method which doesn't require so much trying to "study its sources", but using some of the features of TeX. –  Werner Sep 11 '11 at 5:27
    
Thank you so much for the deep and thorough explanation! –  Alex Vorobiev Sep 12 '11 at 2:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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