0

My question is similar to https://superuser.com/questions/12583/text-to-the-right-of-a-sectiontitle-in-latex-the-normal-case-is-text-comes

I tried using an exmaple from the "The titlesec, titleps and titletoc Packages"

the code is the following

\documentclass[12pt, letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{titlesec}
\usepackage[top=1truein,bottom=1truein,left=1truein,right=1truein]{geometry}

% define section
\titleformat{\section}[leftmargin]
{\normalfont\vspace{12pt}
    \vspace{6pt}%
    \sffamily\bfseries\filleft }
{\thesection\\}{.5em}{}[]
\titlespacing*{\section}{1pc}{1.5ex plus .1ex minus .2ex}{2pc}

\begin{document}


\section*{\large OK} 
this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is example this is an example this is an example this is an example this is an example this is an example this is an example 

\section*{\large Averylongtitle} 
this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is this is an example this is an example this is an example this is an example 
\end{document}

And here is the result. enter image description here

As you can see the title is overlapping with the body text. I tried changing \titlespacing but it does not work.

Note: I prefer the body text to start right after the title at the same horizontal line, just like the first example. However, when the section title name is too long, the name and text overlap.

2
  • 1
    Could you please provide whole code you tried.
    – mmr
    Commented Nov 3, 2021 at 3:39
  • @mmr Thanks for the reply. I have provided the whole code example Commented Nov 3, 2021 at 4:00

2 Answers 2

0

You can use even less code for spacing. Try this:

\documentclass[12pt, letterpaper]{article}
\usepackage{titlesec}

% define section


\begin{document}

    \section{\large Averylongtitle} 
    this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example 

    \titleformat{\section}{\normalfont\Large\bfseries}{\hspace{-5em}\thesection}{1em}{}

    \section{\large Averylongtitle} 
    this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example 
    
\end{document}

Output:

enter image description here

Approach 2: You can try another approach.

\documentclass[12pt, letterpaper]{article}
\usepackage{changepage}
\begin{document}
    \begin{adjustwidth}{-2cm}{0pt}
    \section*{\large Averylongtitle}
    \end{adjustwidth}
    this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example 
\end{document}
7
  • Sorry. Maybe I did not explain clearly, but I want the body text to locate right after the section title, as shown in my new picture. Commented Nov 3, 2021 at 4:27
  • @jchan192 A very long title will start from left margin, right? I think this time your need is served. Answer is updated.
    – mmr
    Commented Nov 3, 2021 at 4:32
  • Thanks for the update . and Yes start from left margin but I will want the body text will start at the same line as the title. Commented Nov 3, 2021 at 4:46
  • @jchan192 Then apply approach 2 and move \end{adjustwidth} at the end of all section text.
    – mmr
    Commented Nov 3, 2021 at 4:50
  • I really appreciate your help. Sorry, I didn't mean same vertical line, but the same horizontal line Commented Nov 3, 2021 at 5:08
0

I guess you want unnumbered sections.

\documentclass[12pt, letterpaper]{article}
\usepackage[top=1truein,bottom=1truein,left=1truein,right=1truein]{geometry}
\usepackage{titlesec}
\usepackage{hyperref}

% define section
\titleformat{\section}[runin]
  {\normalfont\sffamily\bfseries\large}
  {}
  {0pt}
  {\hspace*{-40pt}\makesectiontitle}
\newcommand{\makesectiontitle}[1]{%
  \settowidth{\dimen0}{#1}%
  \ifdim\dimen0<34pt \makebox[34pt][l]{#1}\else #1\fi
}

\titlespacing*{\section}{0pc}{1.5ex plus .1ex minus .2ex}{6pt}

\begin{document}


\section{I}
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 

\section{II}
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 

\section{III}
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 

\section{IIII}
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 

\section{IIIII}
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 

\section{IIIIII}
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 

\section{IIIIIII}
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 

\section{A very long title} 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 
this is an example this is an example this is an example 

\end{document}

If the title is “short”, that is, a minimum space of 6pt is ensured, then a box of the appropriate size is used. Otherwise the title is run in.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .