Page LayoutCenter Block of Text horizontally

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
Stretto
Posts: 3
Joined: Sat Dec 10, 2011 1:59 am

Center Block of Text horizontally

Post by Stretto »

To create a left justified block of text I use

Code: Select all

\begin{center}
\begin{flushleft}
\hspace{1cm}text line 1\\
\hspace{1cm}text line 2\\
\hspace{1cm}text line 2 ...\\
\hspace{1cm}...\\
\end{flushleft}
\end{center}
where the 'center' environment does not actually center in this case but I want it to. If I do not justify then each line is individually centered and the result is that the left side of the text is jagged.

All I want is to be able to center the text as a block with the left side to be aligned.

So
  • text line 1
    text line 2 ...
    ...
I emulate the centering using the hspace but I have to manually center and determine the values. Latex should have no problem figuring it out for me... but how?

Essentially I want the equivalent to \vspace*{\fill} but horizontally and only on a block of text. (I use \vspace*{\fill} to center everything vertically on the page)
Last edited by Stretto on Sat Dec 10, 2011 9:47 pm, edited 2 times in total.

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

Center Block of Text horizontally

Post by Stefan Kottwitz »

Hi Stretto,

welcome to the board!

You could use \parbox, which contains the left aligned text, within a center environment, such as

Code: Select all

\begin{center}
\parbox{5cm}{
  text ccccline 1\\
  text line 2\\
  text line 2 ...\\
  ...\\
}
\end{center}
Stefan
LaTeX.org admin
Stretto
Posts: 3
Joined: Sat Dec 10, 2011 1:59 am

Re: Center Block of Text horizontally

Post by Stretto »

The problem with parbox is I have to specify the size too. I want latex to automatically calculate the size for me else if I change the longest line I'll have to manually figure out a value.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Center Block of Text horizontally

Post by Stefan Kottwitz »

A tabular environment can do the left alignment for you while calculating the width:

Code: Select all

\begin{center}
\begin{tabular}{l}
  text ccccline 1\\
  text line 2\\
  text line 2 ...\\
  ...\\
\end{tabular}
\end{center}
Stefan
LaTeX.org admin
Stretto
Posts: 3
Joined: Sat Dec 10, 2011 1:59 am

Re: Center Block of Text horizontally

Post by Stretto »

Thanks, that will do it.
Post Reply