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.

Using titlesec and/or komascript how do I format all headings so the number is left aligned in the margin, the title to the right, with a horizontal rule underneath it all (from margin to text width edge).

What's left of my hair is about to come out :(

 _____________________________
|                             |
| 3.1  Section Title          |
| -------------------------   |
|      Text width paragraph   |
|      Blah blah blah blah    |
|                             |

  >-<  >------------------<
   |                |
   `-- Margin       ` Text
share|improve this question
    
have you seen Theorem Name/Numbering in Margin or How can I align section titles and list environments in the left margin? you should be able to adapt it to include titlerule which will give you the horizontal line... –  cmhughes Feb 14 '13 at 22:41
    
@cmhughes yes (and many more similar answers), but I haven't found anything that does left alignment and horizontal rule. I'm still working on it, just hoping a guru will through me a bone. –  h0tw1r3 Feb 14 '13 at 22:46
add comment

2 Answers 2

up vote 6 down vote accepted

Here's one possibility using titlesec:

\documentclass{article}
\usepackage[calcwidth]{titlesec}
\usepackage{lipsum}

\newlength\mylen
\setlength\mylen{\dimexpr\oddsidemargin+1in+\hoffset\relax}

\titleformat{\section}
  {\normalfont\Large\bfseries}
  {\llap{\hspace*{-\mylen}\thesection\hfill}}{0em}{}
  [{\makebox[\linewidth][l]{%
    \hspace*{-\mylen}\rule{\dimexpr\textwidth+\mylen\relax}{1pt}}}]

\begin{document}

\section{A Test Section with a Short Title}
\lipsum[4]
\section{A Test Section with a Long Title Spanning More than One Line}
\lipsum[4]

\end{document}

enter image description here

Of course, you can adjust the value for \mylen to get the desired positioning for the numbers; for example, using

\setlength\mylen{\dimexpr\oddsidemargin+\hoffset\relax}

you get

enter image description here

share|improve this answer
add comment

Caveat: margin box size for number is not dynamic.

\documentclass[10pt,twoside,letterpaper,openright]{scrbook}
\usepackage{lipsum}
\usepackage{titlesec}
\titleformat{\section}[block]%
  {\normalfont\scshape\filright}%
  {\makebox[2em][l]{\thesection}}%
  {1em}
  {}[\titlerule]
\titlespacing{\section}{-3em}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\begin{document}
\chapter{Lorem Ipsem}
\section{Lorem Ipsum}
\lipsum[1]
\section{Lorem Ipsum}
\lipsum[1]
\end{document}
share|improve this answer
    
Also consider using \titlespacing*{...} if you wish to prevent the paragraph following the section from being indented. –  davidg Dec 13 '13 at 5:22
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.