4

What I'm trying to do is right-align a new line. In other words, I want to have a block of justified text, as normal, and then I wish to have a line break, with the new line right-aligned like this:

|Lorem ipsum dolor sit amet, consectetur adipiscing elit,|
|sed do eiusmod tempor incididunt ut labore et dolore ma-|
|gna aliqua.                                             |
|                                                    Text|

If the pipes indicate the margins (hyphenation may not respect Latin hyphenation rules, this is just a quick and dirty demonstration).

I tried using the flushright environment, but this, of course, starts a new paragraph, which I do not want:

\documentclass[12pt]{article}
\pagestyle{plain}
\usepackage[margin=1.8cm]{geometry}
\geometry{a4paper}
\usepackage[parfill]{parskip}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
\begin{flushright}
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
  eiusmod tempor incididunt ut labore et dolore magna aliqua.
\end{flushright}

\end{document}

enter image description here

I also tried to use the \raggedleft declaration, but if I try scoping it:

Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \\
{\raggedleft Text}

It does not right-align Text at all.

I also tried using the \hfill command. This does not seem to work at the beginning of a new line, but I was able to quick-and-dirty my way around that problem. Nevertheless, \hfill doesn't quite fill the whole line, for some reason, leaving me with far-from right-aligned Text:

\documentclass[12pt]{article}
\pagestyle{plain}
\usepackage[margin=1.8cm]{geometry}
\geometry{a4paper}
\usepackage[parfill]{parskip}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. \\
\hspace*{0pt}\hfill Text

\end{document}

enter image description here

Possible complications

Firstly, I would, ideally, like to be able to use any solution within an enumerate environment.

Secondly, I was initially trying to define a command for a logical structure. I began with:

\newcommand{\grade}[1]{\newline\textbf{Grade} #1}

And then thought it would look better if I could right-align the grade. As such, ideally, I would like a solution that can be incorporated into the definition of a new command.

  • The \hfill approach seems to work for me if you add \\ after Text. – Guho Oct 21 '15 at 22:47
  • You would need {\raggedleft Text\par}... – Werner Oct 21 '15 at 22:53
  • @Werner that seems, bizarrely, to have centred the text above (modifying my \raggedleft example above by adding \par as you describe) – Au101 Oct 21 '15 at 23:00
4

Issue \par\nopagebreak under zero \parskip, which is obtained by using a group, so the value of \parskip will be restored at \endgroup.

\documentclass[12pt]{article}

\usepackage[parfill]{parskip}

\newcommand{\lastline}[1]{%
  \begingroup\setlength{\parskip}{0pt}\par\nopagebreak
  \raggedleft#1\par\endgroup
}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. 
\lastline{Some text that's flush right.}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.

\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
\lastline{Some text that's flush right.}

\item Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
\end{enumerate}

\end{document}

enter image description here

1

In order for \raggedleft (or any other paragraph-related adjusment) to have effect, you need to insert a "paragraph marker". This could be as simple as leaving a blank line, or issuing a verbatim \par:

enter image description here

\documentclass{article}
\usepackage[parfill]{parskip}

\newcommand{\grade}[1]{%
  \par
  \vspace*{-\parskip}% Correct insertion of paragraph skip due to \par
  {\raggedleft\textbf{Grade} #1\par}}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. \\
\hspace*{0pt}\hfill Text

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
\grade{Text}

\end{document}

Using \\ doesn't insert a paragraph gap. However, in order to make use of \raggedleft one needs a \par. Since \grade issues \par, a vertical correction of \parskip restores any unwanted gap that is inserted between paragraphs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.