How to make environment that right-aligns the text?

In my document, there is a collection of short stories each of which has a date after the end. I want to make an environment for those dates, that would (for now) right-align them, but will probably do something else (probably italicise them, or change their font). That's the reason I am not just using flushright.

I have tried defining that environment as:

\newenvironment{storydate} {\raggedleft}{}

However that did nothing, at least it did not right-align text within that environment.

Why did not that have an effect?

After all, putting \raggedleft before a paragraph does right-align it (and paragraphs after it".

• Welcome to TeX.SX! It's not completely clear what you're about. Can you pleas be more specific? – egreg Feb 4 '18 at 16:41
• @marmot because that's what it does according to documentation and experiment – Srv19 Feb 4 '18 at 16:48
• @egreg "why doesn't that have any effect?", i guess – Srv19 Feb 4 '18 at 16:49
• Why don’t you just use a comment rather than an environment, like \newcommand{\storydate}[1]{\hfill\textit{#1}}? – Jasper Habicht Feb 4 '18 at 16:51
• @Jasper no preferences one way or another – Srv19 Feb 4 '18 at 16:58

Quite likely you don't need an environment, but simply a command.

\newcommand{\storydate}[1]{%
\par                   % ensure vertical mode
\nopagebreak           % we want no page break
\vspace{\medskipamount}% some vertical space
\hspace*{\fill}%       % fill the line
\textit{#1}%           % the date
\par                   % end the new paragraph
}


Full example.

\documentclass{article}

\usepackage{lipsum} % for interesting short stories

\newcommand{\storydate}[1]{%
\par                   % ensure vertical mode
\nopagebreak           % we want no page break
\vspace{\medskipamount}% some vertical space
\hspace*{\fill}%       % fill the line
\textit{#1}%           % the date
\par                   % end the new paragraph
}

\begin{document}

\section*{A nice story}

\lipsum[1-3]

\storydate{February 4, 2018}

\end{document}


Your approach with \raggedleft would work as well, but you should do

\newenvironment{storydate}{\par\raggedleft}{\par}


so that the group initiated by the environment doesn't end before TeX breaks the (single line) paragraph. However this would have the defect of allowing a page break between the end of the story and the date. See the \nopagebreak item in the code above. The \par in front of \raggedleft ensures the last paragraph of the story is broken into lines before the date is typeset.

• Thanks, the bit about preventing page breaks is very important for me; I was about to ask a follow up question about it – Srv19 Feb 5 '18 at 21:24

A simple command will do:

\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ebgaramond}
\usepackage[showframe]{geometry}
\usepackage{lipsum}
\newcommand{\storydate}[1]{\par\noindent\makebox[\linewidth][r]{\itshape#1}\bigskip\par}

\begin{document}

\lipsum[1]
\storydate{\today}
\lipsum[2]

\end{document}


• Thanks, that works. Where did i go wrong, though? – Srv19 Feb 4 '18 at 16:58
• You should have added \hfill before \raggedleft, I guess. – Bernard Feb 4 '18 at 17:11