4

EDIT: I tried the solutions by @Bernard and @egreg.

both the solutions work for multiline headings, that is to say that, the title is centered and all the lines of the titles are at the same distance from the counter \thesection, but not for shorter/normal headings.

I just added,

\section{}
\section{Intro}
\section{Intro intro}

to your respective .tex and it appears that

  • for @Bernard, there is indeed a centering of the void section \section{}, but the two others do not appear visually to be centered, because the text is so short compared to the multiline heading

  • for @egreg, there is no centering of the void section, nor of the short section.

What is needed is exactly what happens now for multilined headings, but for normal headings as well, which means simple centering of the number+title for short headings.

====================== END OF EDIT ===================

I use the article class.

I have multi-line headings for my sections and I want the section number+title to be centered, but when the title has several lines, I want the second lines to begin at the point where the first line begins [after the number section].

I also want to avoid hyphenation in titles, so I was told that \raggedright was the solution.

I was told that titlesec could to do all this, but I failed to get what I want. I also have other commands to put into \renewcommand\section{ which have nothing to do with style. therefore I would like to avoid the use of titlesec and I do not mind to use directly \renewcommand\section{.

\documentclass{article}
\tracinglostchars=2

\setlength\parindent{0pt}
\newcommand*\x{\rule{1cm}{1cm}}


%\usepackage{titlesec}
%\usepackage[extramarks]{titleps}
%\titleformat{\section}{\raggedright}{\textsc{\thesection}}{1em}{\normalfont}
\makeatletter
    \def\@seccntformat#1{\rmfamily\bfseries\upshape\llap{\csname the#1\endcsname\hspace{0.11em}Q8Q\hspace{0.11em}}\normalfont}


    \renewcommand\section{%
    %\clearpage
    % other code...
    %\penalty-9999
    \@startsection {section}{1}{0em}{0\parskip}{10em}{\centering\rmfamily\bfseries\scshape}%
    }

\makeatother
\begin{document}
\section{fontspec warning: "only-luatex-feature" Ignored LuaTeX only feature: 'Renderer=Full/Basic'.
fontspec warning: "only-luatex-feature" Ignored LuaTeX only feature: 'Renderer=Full/Basic'.
fontspec warning: "only-luatex-feature" Ignored LuaTeX only feature: 'Renderer=Full/Basic'.
fontspec warning: "only-luatex-feature" Ignored LuaTeX only feature: 'Renderer=Full/Basic'.
}

\end{document}

currently, it is centered, but, I cannot manage to have centered, plus \raggedright plus ''beginning of the second line at the beginning of the first one'' simultaneously.

  • Is this a bigger project? You might want to use the report class. Changing chapters to do what you want is achieved more easy i think. – Johannes_B Dec 19 '15 at 13:40
  • Sorry, but I can't make a sense out of “centered plus ragged right”. Can you make an approximate representation of what you mean? – egreg Dec 19 '15 at 15:37
  • @egreg I guess a centered minipage (width of .5\linewidth or something) that contains raggedright text. – Johannes_B Dec 19 '15 at 16:19
  • \raggedright and \centering both use \@flushglue in each line to fill out the remaining space, hence no hyphenation. – John Kormylo Dec 19 '15 at 17:03
1

Maybe this is what you'd like to have. Note that \bfseries\scshape does nothing with most fonts; it usually just gives \bfseries.

\documentclass{article}
\usepackage{showframe} % just for the example

\usepackage{titlesec}

\titleformat{\section}
  {\centering}
  {}
  {0pt}
  {\sectionbox{\thesection}}

\titleformat{name=\section,numberless}
  {\centering}
  {}
  {0pt}
  {\sectionbox{}}

\newcommand{\sectionbox}[2]{%
  \parbox[t]{.7\columnwidth}{
    \normalfont\bfseries\raggedright
    \makebox[0pt][r]{#1\quad}%
    #2
  }%
}

\begin{document}

\section*{Some section title that should have a centered appearance,
but be ragged right; it is not clear how good this is}

\section{Some section title that should have a centered appearance,
but be ragged right; it is not clear how good this is}

\end{document}

enter image description here

0

Here is a possible way, with titlesec:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[showframe]{geometry}
\tracinglostchars=2
\usepackage{lettrine, graphicx, ltablex}
\usepackage[x11names]{xcolor}
\setlength\parindent{0pt}

\usepackage[explicit]{titlesec}
%\usepackage[extramarks]{titleps}

\titleformat{\section}[block]{\filcenter\bfseries}{}{0em}
{\begin{tabularx}{0.6\linewidth}{@{}l>{\arraybackslash\raggedright}X@{}}\raisebox{-0.7\height}{\scalebox{4}{\color{SlateGray4!60! LightSkyBlue1}\thesection}} & \parbox[t]{\linewidth}{#1}\end{tabularx}}

\titlespacing{\section}{0ex}{2.5\baselineskip}{3.5\baselineskip}


\usepackage{lipsum}

\begin{document}

\section{fontspec warning: "only-luatex-feature" Ignored LuaTeX only feature: 'Renderer=Full/Basic'.
fontspec warning: "only-luatex-feature" Ignored LuaTeX only feature: 'Renderer=Full/Basic'.
fontspec warning: "only-luatex-feature" Ignored LuaTeX only feature: 'Renderer=Full/Basic'.
fontspec warning: "only-luatex-feature" Ignored LuaTeX only feature: 'Renderer=Full/Basic'.
}
\lipsum[1]

\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.