Text FormattingCentering text

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Txukolatex
Posts: 3
Joined: Mon Jan 30, 2017 10:39 am

Centering text

Post by Txukolatex »

Hi to all,

I am trying to do something as simple as centering text in a page but is impossible for me. It does not center the last line of the first paragraph and I don't understand why.

This is the minimum not working example:

Code: Select all

\documentclass[12pt, a4paper]{book}
\hyphenpenalty=10000
\exhyphenpenalty=10000
\renewcommand{\baselinestretch}{1.5}

\begin{document}
\vspace*{\fill}

\begin{center}
\centering
{
\textbf{\Huge{XXXXXX XXXXXXXXX XXXXXXXX XXXXXXXXX XXXXX}}
\newline
\newline
\LARGE XX XX XXXXX XX XXXX\par
}
\end{center}
\vspace*{\fill}

\end{document}
Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Centering text

Post by Stefan Kottwitz »

Welcome to the forum.

That's a very good minimal example, as it shows the problem in a short way. The simple answer is: don't use \newline except you really want to end a line (and definitely not a paragraph) and never use it to get vertical whitespace. You could replace it by \par or by an empty line.

And don't use \centering within a center environment, just choose either of them.

Minimal working example:

Code: Select all

\documentclass[12pt, a4paper]{book}
\hyphenpenalty=10000
\exhyphenpenalty=10000
\renewcommand{\baselinestretch}{1.5} 
\begin{document}
\vspace*{\fill}
\begin{center}
\Huge\bfseries
XXXXXX XXXXXXXXX XXXXXXXX XXXXXXXXX XXXXX
\par
\vspace{\baselineskip}
\LARGE XX XX XXXXX XX XXXX\par
\end{center}
\vspace*{\fill} 
\end{document}
Stefan
LaTeX.org admin
Txukolatex
Posts: 3
Joined: Mon Jan 30, 2017 10:39 am

Centering text

Post by Txukolatex »

Thanks. I knew that was absurd to put center and centering at the same time, but they didn't work separated.

The worst thing with latex is that each command always is doing more things than it is supposed to do.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Centering text

Post by Johannes_B »

If you want to create a title page, please have a look at the wikibook page. A few examples are linked at the bottom.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Centering text

Post by Stefan Kottwitz »

Txukolatex wrote:each command always is doing more things than it is supposed to do.
So you can tell us a lot of such commands doing unexpected things? All commands, you said. I'm curious, can you give some examples?

I think, commands do what they are supposed to do, not more. It's possible that users don't know, but it can be read in documentation when there's time. I agree that it's not easy for a beginner or occasional LaTeX user, but it's not the solution to say something like above just because you don't know better. We can provide explanation here, if you need some details about a command.

Stefan
LaTeX.org admin
Txukolatex
Posts: 3
Joined: Mon Jan 30, 2017 10:39 am

Centering text

Post by Txukolatex »

I have just give you an example. The command \newline alters the centering of the previous line.

And take into account that I am not trying to attack a very good tool as Latex or justify my ignorance, which I assume.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Centering text

Post by Stefan Kottwitz »

Txukolatex wrote:The command \newline alters the centering of the previous line.
That's not the case, the reason is: centering works based on paragraphs, so it requires a paragraph end to center also the last line.

Stefan
LaTeX.org admin
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Centering text

Post by Stefan Kottwitz »

Here is an example:

Code: Select all

\documentclass[12pt, a4paper]{book}
\hyphenpenalty=10000
\exhyphenpenalty=10000
\renewcommand{\baselinestretch}{1.5}
\begin{document}
\vspace*{\fill}
\bfseries
{% next lines centered?
\centering
\Huge
XXXXXX XXXXXXXXX XXXXXXXX XXXXXXXXX XXXXX
% activate, that is, comment the next line out to fix:
%\par
}% previous lines centered?
\par
\vspace{\baselineskip}
\begin{center}
\LARGE XX XX XXXXX XX XXXX\par
\end{center}
\vspace*{\fill}
\end{document}
The \centering in the curly braces doesn't work, because there is no paragraph break in the {...} group where centering should be. If you remove the % before \par, centering works. You would also notice that line spacing works based on the paragraph, once you have the \par in the group, the line space is like it should be for \Huge.

Just to explain, why centering needs a paragraph break and not a \newline.

Stefan
LaTeX.org admin
Post Reply