titleformat 标题和正文段落之间的水平间距

titleformat 标题和正文段落之间的水平间距

我的问题类似于https://superuser.com/questions/12583/text-to-the-right-of-a-sectiontitle-in-latex-the-normal-case-is-text-comes

我尝试使用“titlesec、titleps 和 titletoc 包”中的示例

代码如下

\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}

结果如下。 在此处输入图片描述

如您所见,标题与正文重叠。我尝试更改,\titlespacing 但没有效果。

笔记:我更喜欢正文紧跟在标题之后,在同一水平线上,就像第一个例子一样。但是,当部分标题名称太长时,名称和文本会重叠。

答案1

您可以使用更少的代码来设置间距。试试这个:

\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}

输出:

在此处输入图片描述

方法 2:您可以尝试另一种方法。

\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}

答案2

我猜你想要的是未编号的部分。

\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}

如果标题很“短”,即确保最小间距为 6pt,则使用适当大小的框。否则标题将排成一行。

在此处输入图片描述

相关内容