# hfill alignment doesn´t work

I used the code below to format some information but the last line before the red line is not right aligned. I cant find the problem in the code. Any suggestion? Thanks.

\documentclass[11pt,a4paper]{article}
\usepackage{color}
\usepackage[T1]{fontenc}

\setlength{\parindent}{0em}

\begin{document}
\begin{samepage}
{\color{blue}\hrule height 2pt}
\vspace{.1\baselineskip}
{\Large Some important text}
\hfill
{\large \textit{And more}} \\
{\small More some text}
\hfill \textit{Important person:}~Me \\[.3\baselineskip]
{Year:~2018, Volume:~1, Nr.:~1}
\hfill
Date 1: YYYY-MM-DD\\
{Classification:~A}
\hspace{\fill}
Date 2: YYYY-MM-DD
\vspace{.3\baselineskip}
{\color{red}\hrule height 2pt}
\end{samepage}
\end{document}


You have to protect end of lines in a few places, otherwise a space might appear there.

\documentclass[11pt,a4paper]{article}
\usepackage{color}
\usepackage[T1]{fontenc}

\setlength{\parindent}{0em}

\begin{document}
\begin{samepage}
{\color{blue}\hrule height 2pt}
\vspace{.1\baselineskip}
{\Large Some important text}
\hfill
{\large \textit{And more}} \\
{\small More some text}
\hfill \textit{Important person:}~Me \\[.3\baselineskip]
{Year:~2018, Volume:~1, Nr.:~1}
\hfill
Date 1: YYYY-MM-DD\\
{Classification:~A}
\hspace{\fill}
Date 2: YYYY-MM-DD% <- Here
\vspace{.3\baselineskip}% <- Here
{\color{red}\hrule height 2pt}% <- Here (this one isn't necessary, actually)
\end{samepage}
\end{document}


### Edit: A comment at the end of the line? Seriously?

This is something that everyone already had problems with (the "everyone" link is the best :P).

What happens here is that (in normal situations) TeX interprets a line break as a space character, so when you type:

abc
def


the result is the same as if you had typed:

abc def


(notice the space after c). But when you type:

abc%
def


TeX ignores everything that comes after the %, and this includes the line break, so it is essentially the same as:

abcdef


And that's why you had a space after the DD in your code.

When you have the time, I suggest you read koleygr's answer here and if you want a more lengthy explanation, my answer here.

• Last one isn't necessary ... but not a bad practice too – koleygr Mar 21 '18 at 20:06
• (+1)... I would add some text about "tokens" just because on a similar problem I had, this helped me to find some resources to understand... Here is the answer: tex.stackexchange.com/a/376959/120578 [I haven't read The TeXbook yet :( ] – koleygr Mar 21 '18 at 20:12
• @koleygr Nice answer there. I'll add something here :) – Phelype Oleinik Mar 21 '18 at 20:15
• @koleygr Done :) – Phelype Oleinik Mar 21 '18 at 20:45

You can replace \vspace{.3\baselineskip} by \vskip.3\baselineskip and your problem is solved. The \vskip TeX primitive is much more natural than LaTeX's \vspace which does not end the paragraph but insert vertical space between lines of current paragraphs (using \vadjust primitive).

A) What happens when you type

 end words.
\vskip.3\baselineskip


Then \vskip ends the paragraph (using internal \par inserted before \vskip) and this \par consumes the space from end line. This is normal behaviour of processing of each paragraph.

B) What happens when you type

end words.
\vspace{.3\baselineskip}
{\color{red}\hrule height 2pt}


Then we have end words. followed by space (from end line) followed by \vadjust object (from \vspace) followed by space (from second end line) followed by color setting (from \color macro) followed by \par, which is inserted by \hrule primitive. The second space is consumed by this \par but the first space is not consumed.

I'd use a minipage rather than samepage. You are adding several unwanted spaces in your code; for instance, x \hspace{<length>} adds a space between x and the glue. In your case it's not very important, as you're adding infinite glue, in other cases it would be.

Also endlines count as spaces; in some cases they're removed, in other cases they aren't. It's better to have less compact code that shows more clearly your intentions.

\documentclass[11pt,a4paper]{article}
\usepackage{xcolor}

\begin{document}

\noindent
\begin{minipage}[b]{\textwidth}
{\color{blue}\hrule height 2pt}

\vspace{.3\baselineskip}

{\Large Some important text\hfill
\textit{And more}\par}%<-- to preserve \par at the font size change

{\small More some text}\hfill
\textit{Important person:}~Me

\vspace{.3\baselineskip}

Year:~2018, Volume:~1, Nr.:~1\hfill
Date 1: YYYY-MM-DD

Classification:~A\hfill
Date 2: YYYY-MM-DD

\vspace{.3\baselineskip}

{\color{red}\hrule height 2pt}
\end{minipage}

\end{document}


A trailing space is removed at a \par command (automatically inserted by a blank line). It's better if \vspace is issued between paragraphs like in the code above. Also spaces/endlines after control sequences (in the code above, \hfill) are not even seen by TeX.

I set the whole first line in \Large size, as \large in the part to the right would be almost indistinguishable; note \par before the closing brace, which ensures the correct interline spacing for \Large` is used.