I am currently writing LaTeX.js to translate LaTeX to HTML in the browser. So I want it to be as exact as possible. However, TeX's alignment implementation together with LaTeX's \\ make that hard.
Simple start:
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud\\ % centered!
exercitation ullamco laboris\\ % centered!
nisi ut aliquip ex ea commodo consequat.\raggedleft
You would think that the whole second paragraph will now be flushright because \leftskip affects the whole current paragraph (and all the following ones). But that is not what happens; instead, the second- and third-last lines are now centered! Why is that so? Is that because \\ inserts some fill glue? I did not find or understand how LaTeX really implements \\.
Assuming that glue is the reason, why then wasn't an infinitely larger glue chosen for the \leftskip to prevent centering?
Then I found that if you move the \raggedleft to the beginning of the paragraph, it works as expected:
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
\raggedleft
Ut enim ad minim veniam, quis nostrud\\ % fine
exercitation ullamco laboris\\ % fine
nisi ut aliquip ex ea commodo consequat.
How do you explain that? Is that a bug or is that intentional? I can't possibly make LaTeX.js behave that way :(
But it doesn't end there. Let's put it in a group:
{
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud\\
exercitation ullamco laboris\\
nisi ut aliquip ex ea commodo consequat.\raggedleft
}
Already \raggedleft doesn't do anything anymore. It seems that is because there is no paragraph that ends. And when the paragraph ends after the group, it has the values of \leftskip and \rightskip already reset. Correct?
If so, that would be understandable. Always the last paragraph in a group would have the alignment reset. But again, surprise if you add \\:
{
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud\\
exercitation ullamco laboris\\
nisi ut aliquip ex ea commodo consequat.\raggedleft
Lorem ipsum dolor sit amet,\\ % ?! raggedleft
consectetur adipiscing elit,\\ % ?! raggedleft
sed do eiusmod tempor incididunt. % justified
}
Here, I would have expected the the whole last paragraph to be justified, but it seems like \\ is acting as if it would start a new paragraph?! Except it doesn't because \parindent is not applied.
What is going on here?
EDIT: as David asked for it, there really isn't anything else to it. Here is the full document:
\documentclass{article}
\begin{document}
{
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud\\ % centered
exercitation ullamco laboris\\ % centered
nisi ut aliquip ex ea commodo consequat.\raggedleft
Lorem ipsum dolor sit amet,\\ % ?! raggedleft
consectetur adipiscing elit,\\ % ?! raggedleft
sed do eiusmod tempor incididunt. % justified
}
\end{document}

\\, so it should be at the begin/before the first\\. – Ulrike Fischer Dec 10 '17 at 14:41\@centercrdoes and why it leads to the above results? – MiB Dec 10 '17 at 15:03