3

I would like to add \linewidth-sized lines above and below my chapter titles (similar to borders above/below the 'paragraph' in MS Word)

I tried the following, which somewhat approximates the desired look (not perfect, and I don't really understand the \kern-.75\ht\strutbox stuff:

\documentclass[
    draft,
    parskip=full,
]{scrreprt}

\makeatletter
\renewcommand{\chapterlinesformat}[3]{%
\parbox[t]{\linewidth}{%
\raggedchapter%
\noindent\rule{\linewidth}{.6pt}%
\par\@hangfrom{#2}{#3}%
\par\kern-.75\ht\strutbox\rule{\linewidth}{.6pt}%
}%
}
\makeatother

\begin{document}
\chapter{test}
\end{document}

However, this causes overfull hboxes on every chapter title (and makes it difficult to see if I have actual overfull hboxes), even though the lines do not extend past the width of the text area.

I also tried the solution in KOMA-Script line above and below chapter title (lowermost part of the accepted answer), however the lines are very far from the text and the overfull hbox issue persists (if only on the lower rule).

Joe
  • 416
  • I can tell you why it happens. It's go to do with parskip being full. What I guess komascript is doing is adding a rule or something at the end of each paragraph to make sure it has at least one em left at the end (though I haven't checked). If you add \par after your parbox the overfull line is solved (but the spacing is off). I'd be inclined just to live with it: the output (once draft mode is off) is fine. – Paul Stanley Dec 19 '18 at 17:48
  • As mentioned in the question, “living with it” makes it very difficult to find real issues later. – Joe Dec 19 '18 at 17:49
  • You need to add a \par after the \parbox, to avoid that the wrong \parfillskip is used. – Ulrike Fischer Dec 19 '18 at 17:58
  • @UlrikeFischer adding \par shifts the entire thing. – Joe Dec 20 '18 at 07:55

2 Answers2

3

You need a \par at the end or a wrong parfillskip will be used. To avoid the shift you can locally set parskip to zero:

\documentclass[
    draft,
    parskip=full,
]{scrreprt}

\makeatletter
\renewcommand{\chapterlinesformat}[3]{%
\parskip=0pt
\noindent\rule{\linewidth}{.6pt}%
\par\@hangfrom{#2}{#3}%
\par\kern-.75\ht\strutbox
\noindent\rule{\linewidth}{.6pt}%
\par
}
\makeatother

\begin{document}
\chapter{test}

bbbb

\chapter{test test test test test some text text text text test xtt }

bbbb\par abc

\end{document}
Ulrike Fischer
  • 327,261
  • I'm getting inconsistent spacing from the top of my textarea and the top line. Sometimes even negative... (in the real-world document) – Joe Jan 09 '19 at 11:42
  • Having found a workaround for the inconsistent spacing (see https://tex.stackexchange.com/questions/476356/understanding-stretchy-negative-vertical-space-above-framed-chapter-titles) I will accept this as it was the earlier answer. – Joe Feb 23 '19 at 17:56
2

From the KOMA-Script documentation of option parskip=full:

one line vertical space between paragraphs; there must be at least 1 em free space in the last line of a paragraph

\chapterlinesformat is called inside a group with \parskip set to 0pt and \parfillskip set to 0pt plus 1fil. But this group is followed by a \par command using your parskip=full setting and there is no 1em free space after the \parbox ...

If you do not need the 1em free space in the last line of paragraphs, you could use parskip=full-:

\documentclass[
    draft,
    parskip=full-,% <- changed
]{scrreprt}
\usepackage{showframe}% to show the page layout
\usepackage{lipsum}% only for dummy text

\makeatletter
\renewcommand{\chapterlinesformat}[3]{%
\parbox[t]{\linewidth}{%
\raggedchapter%
\noindent\rule{\linewidth}{.6pt}%
\par\@hangfrom{#2}{#3}%
\par\kern-.75\ht\strutbox\rule{\linewidth}{.6pt}%
}%
}
\makeatother

\begin{document}
\chapter{test}
\lipsum[1-2]
\chapter{test test test test test some text text text text long text }
\lipsum[3-4]
\end{document}

Result:

enter image description here

But note there is really no white space in the last line of the first paragraph.

You could also use \makebox[\dimexpr\linewidth-1em\relax][l]{...} or better \makebox[.65\linewidth][l]{...} (ensures that it works for parskip=+ and parskip=full*, too):

\documentclass[
    draft,
    parskip=full
]{scrreprt}
\usepackage{showframe}% to show the page layout
\usepackage{lipsum}% only for dummy text

\makeatletter
\renewcommand{\chapterlinesformat}[3]{%
  \makebox[.65\linewidth][l]{% <- added
    \parbox[t]{\linewidth}{%
      \raggedchapter
      \noindent\rule{\linewidth}{.6pt}%
      \par\@hangfrom{#2}{#3}%
      \par\kern-.75\ht\strutbox\rule{\linewidth}{.6pt}%
    }%
  }% <- added
}
\makeatother


\begin{document}
\chapter{test}
\lipsum[1-2]
\chapter{test test test test test some text text text text long text}
\lipsum[3-4]
\end{document}

Result:

enter image description here

esdd
  • 85,675