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.

In attempting to answer Indent all the normal text, I had to resort to using the titlesec package to unindent the section headings. With \let\OldSection\section, I was sure that either

\def\section#1{\hspace*{-\LeftMargin}\OldSection{#1}}

or

\def\section#1{\endChangeMargin\OldSection{#1}\ChangeMargin{\LeftMargin}{\RightMargin}}

would result in normal placement of section headings but they don't. Why is that?

Furthermore, there is also a vertical space prior to the section heading. This is also visible in the solution to the linked question where the the titlesec package was used.

So, how can one adjust the horizontal spacing of the section headings without the vertical spacing being changed?

Code:

\documentclass{article}
\usepackage{showframe}
\usepackage{lipsum}

\newcommand*{\LeftMargin}{0.5cm}%
\newcommand*{\RightMargin}{0.0cm}%

%% http://tex.stackexchange.com/questions/588/how-can-i-change-the-margins-for-only-part-of-the-text
\def\ChangeMargin#1#2{\list{}{\rightmargin#2\leftmargin#1}\item[]}%
\let\endChangeMargin\endlist%

% Not sure why these did not work
\let\OldSection\section
%\def\section#1{\hspace*{-\LeftMargin}\OldSection{#1}}%
\def\section#1{\endChangeMargin\OldSection{#1}\ChangeMargin{\LeftMargin}{\RightMargin}}%

%%%% The following achieves the desired result, but why does the above not work
%%%% http://tex.stackexchange.com/questions/25082/customizing-indentation-in-section-and-subsection-headings
%\usepackage{titlesec}
%\titlelabel{\hspace*{-\LeftMargin}\thetitle~}


\begin{document}
\ChangeMargin{\LeftMargin}{\RightMargin}
\section{Section Name}
\lipsum*[1-3]
\endChangeMargin
\end{document}
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

The

\def\section#1{\hspace*{-\LeftMargin}\OldSection{#1}}

doesn't work because \OldSection issues a \par. So the \hspace is actually above the section title.

The correct way to change the number is to adapt \@seccntformat so that the counter sticks in the margin. E.g.:

\documentclass{article}
\usepackage{showframe}
\usepackage{lipsum}
\makeatletter
\def\@seccntformat#1{\protect\makebox[0pt][r]{{\csname the#1\endcsname\quad}}}
\makeatother
\begin{document}

\section{Section Name}
\lipsum*[1-3]
\end{document}
share|improve this answer
    
Why not \def\@seccntformat#1{\hspace*{-\@totalleftmargin}\csname the#1\endcsname\quad}? –  egreg Oct 29 '12 at 11:52
    
@egreg: Well at first in my example \@totalleftmargin gives 0pt, so the \hspace doesn't do much. At second as I understood the question Peter wants to align the text of the section title with the margin and so it is necessary that the representation of the counter has width 0. –  Ulrike Fischer Oct 29 '12 at 12:51
    
I believe that your code answers the original question rather than Peter's. –  egreg Oct 29 '12 at 12:58
    
Thanks that explains it. But what was needed is \def\@seccntformat#1{\protect\hspace*{-\LeftMargin}{\csname the#1\endcsname\quad}} to make things align correctly. As @egreg suggested that completes the original question, so you should post an answer there. –  Peter Grill Oct 29 '12 at 17:47
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.