How to prevent page breaks and force indentation in inner paragraphs of a command?

I'm trying to write a diary of a journey I did in the past but I'm facing a problem with paragraph identation and page breaks. I've written the following command (\EntradaDiario)

\newcommand{\Separador}{
{\noindent\begin{center}
$\ast~\ast$
\end{center}}
}
{\footnotesize \mbox{\textit{#6} : #2 $\xrightarrow{#3~\text{km.}}$
#5.}\hfill\makebox[1.6cm][r]{\textit{#4~km.}}}
% <-------- Page breaks allowed here?
%% Body of the diary for one day: -------------------------------------------------
\textbf{#1~}$\rightarrowtail$~#7
}


Which when used, results in something like this:

\EntradaDiario{Title}{Location1}{10}{20}{Location2}{Date}{\lipsum[1]}


I do want the header and the first line of the body section to have no indentation, whereas the rest of the text (even if it has several paragraphs) has to be indented. My problem is that the line break between the header and body sections can cause a page break, and I want to avoid that. What would be the best way to prevent this behaviour?

Also, how could I force new paragraphs inside the body section (the actual text) to also have the first line with the same indentation than the rest of the text?

• Just to clarify: There are parts where you are using the complete textwidth? Have you checked packages like scrextend and the addmargin environment? – Johannes_B Sep 3 '15 at 17:18

I use xgalley in the example below to format the page layout.

In order to keep the "header" and "first line of body text" together, I use needspace, requesting at least the availability of 2\baselineskip. You might want to change this slightly, depending on your layout and how the height of \xrightarrow spans vertically, but the principle is sound.

\documentclass{article}
\usepackage{xgalley,amsmath,amssymb,needspace}
\usepackage{lipsum,geometry}% Just for this example
\geometry{showframe,paperheight=25\baselineskip}

% http://river-valley.zeeba.tv/media/conferences/tug-2015/0302-Joseph-Wright/
\ExplSyntaxOn
\cs_new_eq:NN \SetAbsMargins \galley_margins_set_absolute:nn  % left, right
\cs_new_eq:NN \SetRelMargins \galley_margins_set_relative:nn  % left, right
\cs_new_eq:NN \SetAllShape   \galley_parshape_set_multi:nnnN  % Normal, left, right, resume
\cs_new_eq:NN \SetOneShape   \galley_parshape_set_single:nnnN % Normal, left, right, resume
\cs_new_eq:NN \CutoutLeft    \galley_cutout_left:nn           % Normal, indents
\cs_new_eq:NN \CutoutRight   \galley_cutout_right:nn          % Normal, indents
\cs_new_eq:NN \True          \c_true_bool
\cs_new_eq:NN \False         \c_false_bool
\ExplSyntaxOff

\noindent\hfill$\ast~\ast$\hfill\mbox{}%
}
\par
\Needspace{2\baselineskip}
\hspace*{-3em}% Indent into margin
{\footnotesize \mbox{\textit{#6} : #2 $\xrightarrow{#3~\text{km.}}$
#5.}\hfill\makebox[1.6cm][r]{\textit{#4~km.}}}
\par\nobreak
%% Body of the diary for one day: -------------------------------------------------
\hspace*{-3em}\textbf{#1~}$\rightarrowtail$~\ignorespaces

\setlength{\parindent}{0pt}
\AtBeginDocument{%
\SetAbsMargins{3em}{0pt}
}

\begin{document}

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\lipsum[7]

\end{document}


You'll also see that I've converted EntradaDiaro to be an environment rather than a macro. It made more sense since it may contain (multi-)paragraph text.

• Nice, \Needspace did the job! Thanks a lot! – Carles Araguz Sep 4 '15 at 11:34

Here is a relatively package free approach. Page breaking is prevented for three lines by overlaying the first three lines with an large strut. I don't know how needspace works, but have been using this trick for some time now.

The hanging indentation was achieved using \leftskip and negative \hspace (\hangindent only affects one paragraph). I changed it to an environment for the same reasons as Werner. I simplified \Separador just to fit it into one line.

\documentclass{article}
\usepackage{showframe}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}

\newcommand{\Separador}{\par\noindent\hfil{$\ast~\ast$}\par}

\leftskip=\bibindent
\hspace*{-\leftskip}%
{\footnotesize\mbox{\textit{#6} : #2 $\xrightarrow{#3~\text{km.}}$
\hspace*{-\leftskip}\textbf{#1~}$\rightarrowtail$~\ignorespaces}