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 created a macro to insert diary-like right-aligned paragraphs.

\newcommand{\diarydate}[1]{\begin{flushright}\textit{#1}\end{flushright}}

Example:

\diarydate{New York, August 1992}

enter image description here

This looks great at the top of the page, but not if there's a paragraph preceding it. Therefore, I want to add vertical spacing before the diarydate text if it is located between two paragraphs, but not when it's at the top of a page or right after a heading.

Is there an easy command or macro that covers all those use cases?

share|improve this question
    
use \addvspace space added by \addvspace is merged, if the section heading adds some and this ads some you get the maximum, not the sum –  David Carlisle Feb 10 '13 at 13:51
    
@DavidCarlisle Thanks, but in this case I don't want the maximum. Rather, I want the diarydate not to add any space at all when following a heading. –  Danilo Bargen Feb 10 '13 at 14:16

1 Answer 1

up vote 6 down vote accepted

You want to use a sectioning command, that will guarantee that no page break will be taken between the diary date and the following text. For instance

\documentclass{article}
\usepackage[pass,showframe]{geometry} % to show the page frame
\usepackage{lipsum}
\makeatletter
\newcounter{diary} % to keep LaTeX happy
\newcommand{\diarymark}[1]{} % ditto
\newcommand\diarydate{%
  \@startsection{diary}%
    {10}%        level for secnumdepth and tocdepth
    {\z@}%       indentation
    {\topsep}%   space before
    {\topsep}%   space below
    {\raggedleft\normalfont\itshape}% format of the text
}
\makeatother
\begin{document}
\lipsum[2]
\diarydate{New York, August 1992}
\lipsum[2]
\section{A section}
\diarydate{New York, August 1992}
\lipsum[2]
\section{B section}
\lipsum[2]
\newpage
\diarydate{New York, August 1992}
\lipsum[2]
\end{document}

The space in the case of \section followed by \diarydate is the same as the space between a section title and normal text.

If you change the "space before" line into

    {-\topsep}%   space before

the first line after \diarydate will not be indented. I used \topsep as it is the vertical space used by flushright.

Here are the pictures.

First page

enter image description here

Second page

enter image description here


If you really want to suppress vertical spacing after a heading, you can do it in two steps:

\makeatletter
\newcounter{diary} % to keep LaTeX happy
\newcommand{\diarymark}[1]{} % ditto
\newcommand\@diarydate{%
  \@startsection{diary}%
    {10}%
    {\z@}%
    {\topsep}%
    {\topsep}%
    {\raggedleft\normalfont\itshape}%
}
\newcommand{\diarydate}{%
  \par % ensure vertical mode
  \if@nobreak % we're after a heading
    \vskip-\lastskip % suppress the space added by the heading
    \vskip-\topsep % suppress the space added by \@diarydate
  \fi
  \@diarydate}
\makeatother

and use \diarydate as before.

share|improve this answer
    
Thanks for the extensive answer. What I don't understand: Why do you define a diarymark command, but never use it? Also, pdflatex complains about "Illegal parameter number in definition of \diarydate". If I add a [1] parameter count, another error message pops up: "LaTeX Error: Something's wrong--perhaps a missing \item" –  Danilo Bargen Feb 10 '13 at 14:48
    
The necessity for \diarymark comes from the use of \@startsection. I get no error with either version. –  egreg Feb 10 '13 at 15:07
    
thanks, it was an error on my side :) It works great now (although I use 7mm as space before, instead of \topsep. –  Danilo Bargen Feb 10 '13 at 15:23
    
Is there a way to prevent the items showing up in the table of contents? –  Danilo Bargen Feb 20 '13 at 14:56
    
@DaniloBargen Add \newcommand\l@diary[2]{} before \makeatother –  egreg Feb 20 '13 at 15:08

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.