Text FormattingTitle alignment

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Ranalli13
Posts: 5
Joined: Tue Mar 23, 2021 7:59 pm

Title alignment

Post by Ranalli13 »

Dear Latex Community,

I'm struggling formatting the titles alignment in latex.

I'm using the report document and i want to have some sections to be aligned to the left and some in the center.

I used the below package to define all the title alignment to the center.

Code: Select all

\usepackage{titlesec}
\titleformat{\section}{\bfseries\Large\centering}{\thesection.}{\hspace{0.1cm}}{} 
Now i want to know If it is possible to use this package and just add something to the sections that i want to be aligned to the left or what do to in order to have some sections on the left and some on the center?

Thanks in advance for the support!

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

Title alignment

Post by Stefan Kottwitz »

Hello,

as Bartman said in the other topic about the font issue, please always provide a Infominimal working example that contains the problem, because then we can test and fix or improve it. Otherwise ever reader has to type a whole document to provide a solution that's tested and works. I did this one time now, please next time show it with a small but complete code like this:

Code: Select all

\documentclass{report}
\usepackage{titlesec}
\newcommand*{\myalignment}{\centering}
\titleformat{\section}{\bfseries\Large\myalignment}{\thesection.}{0.1cm}{} 
\begin{document}
\chapter{One}
\section{Centered heading}
\renewcommand*{\myalignment}{\raggedright}
\section{Left aligned heading}
\renewcommand*{\myalignment}{\centering}
\section{Another centered heading}
\end{document}
Here you can see how one may do it as one possible way. By the way I replaced \hspace{0.1cm} by 0.1cm since at that point a length value is expected, not a space command.

You can see an advantage of a complete code example here: one can edit and compile it right here in the forum by clicking the "Run LaTeX here" button. So one can run LaTeX for testing the code even on a phone. Code is marked by selectin it and clicking the "Code" button, when editing a post here.

Stefan
LaTeX.org admin
Ranalli13
Posts: 5
Joined: Tue Mar 23, 2021 7:59 pm

Title alignment

Post by Ranalli13 »

Dear Stefan,

Thanks for the support. My problem is solved.

I will not forget to insert a minimal working example next time.

All the best.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Title alignment

Post by Stefan Kottwitz »

Yes, it's incredibly easy to fix a problem when the code is here. I can simply edit the code and run it right here, to see if it an issue can be solved in a way.

See you next time when a question arrises. :-)

Stefan
LaTeX.org admin
Post Reply