5

I'd like to manipulate the vertical space under a custom command I defined. How can I do that?

(all these years and I actually have never looked into this before...)

Picture

enter image description here

MWE

\documentclass{scrartcl}

\usepackage{
setspace,
lipsum
}

\newcommand{\something}[1]{\begin{flushleft}\large\sffamily{#1}\end{flushleft}}

\begin{document}
\onehalfspacing
\lipsum[1]
\something{Words}
\lipsum[2]
\end{document}

Solution

\makeatletter
\newcommand{\something}[1]{%
    \par\addvspace{\topsep}%
    \noindent\begingroup\raggedright\sffamily#1\par\nobreak\addvspace{0.15\baselineskip}\endgroup
    \@afterheading\@afterindentfalse
}
henry
  • 6,594
  • This depends on the settings of the flushleft environment. I am unsure, which skipping it's introduced by it. Perhaps something like \topskip etc. –  Sep 20 '14 at 20:14
  • 1
    http://tex.stackexchange.com/questions/27696/extra-intervals-before-and-after-flushright-environment –  Sep 20 '14 at 20:22

2 Answers2

4

You don't want to use flushright, but simply \raggedright; also you're missing a \nopagebreak.

\documentclass{scrartcl}

\usepackage{lipsum}

\newcommand{\something}[1]{%
  \par\addvspace{\topsep}%
  \noindent\begingroup\raggedright\sffamily#1\par\endgroup
  \nopagebreak
  \noindent\ignorespaces
}

\begin{document}

\lipsum[1]

\something{Words}
\lipsum[2]

\end{document}

Don't leave any blank line between \something{...} and the text that follows.

enter image description here

I removed \onehalfspacing because it's against my religion. ;-) Add it, if you really have to spoil your document. ;-)

A perhaps better solution that also allows blank lines after \something{...} employs the internals used after section headings.

\documentclass{scrartcl}

\usepackage{lipsum}

\makeatletter
\newcommand{\something}[1]{%
  \par\addvspace{\topsep}%
  \noindent\begingroup\raggedright\sffamily#1\par\nobreak\endgroup
  \@afterheading\@afterindentfalse
}
\makeatother

\begin{document}

\lipsum[1]

\something{Words}

\lipsum[2]

\end{document}
egreg
  • 1,121,712
  • This seems to be quite sound as well, but in general I do not intend to fully delete the vertical space, just manipulate it as I said in the op. Thank you for the second solution, I like this one more than the first one because I also want to leave blank line in the code. :) – henry Sep 21 '14 at 07:57
  • Ah figured it out myself, I edited it into \sffamily#1\par\addvspace{0.15\baselineskip}\nobreak\endgroup. Or some other factor might prove to look ok as well. :) – henry Sep 21 '14 at 08:13
  • @henry \nobreak should go before the spacing. – egreg Sep 21 '14 at 09:17
  • Ok. Thanks. Will update the op with what I was looking for. – henry Sep 21 '14 at 09:45
  • I wanted to test and enable the indentation after the command again. Deleting \@afterheading\@afterindentfalse lead to a satisfactory result. Is the definition still ok then? (deleting onle \@afterindentfalse didn't produce the indentation somehow) – henry Sep 21 '14 at 10:27
  • @henry Just \@afterindentfalse should be removed: \@afterheading sets up the “no page break” mechanism. – egreg Sep 21 '14 at 10:29
  • Hm, ok. Weird, it works in the MWE but not in my main document. :/ – henry Sep 21 '14 at 10:34
3

flushleft is defined as trivlist, as such it uses the topsep, \partopsep and \parskip lengths (added!) for the top and the bottom spacing to the next text content. In order to reduce the spacing below, it's best to set this elastic lengths to 0pt within a \begingroup...\endgroup pair, but issue \vspace{\topsep} before, in order to maintain some distance to the text above.

\documentclass{scrartcl}

\usepackage{setspace,
lipsum%
}


\newcommand{\something}[1]{\begingroup
  \vspace{\topsep}% Use the old top distance
  \setlength\partopsep{0pt}\setlength\topsep{0pt}\setlength\parskip{0pt}% Set the top seps to zero
  \begin{flushleft}\large\sffamily{#1}%
  \end{flushleft}\endgroup%
}%


\begin{document}
\onehalfspacing
\lipsum[1]
\something{Words}
\lipsum[2]
\something{Other Words}
\lipsum[3]
\end{document}

enter image description here