titlesec 问题

titlesec 问题

我正在尝试使用该titlesec包重新定义我的一些部分和子部分命令。我希望\subsection它后面的文本换行,但如果它有一个标题定义,\subsection{definitions}那么它应该像平常一样让标题跟随,并且标题也换行在文本中。我不知道该怎么做。

下面是我的问题的一个示例 tex 文件。

\documentclass[10pt]{article}

\usepackage{titlesec}

\titleformat{\section}[hang]{\large}{\thesection.0}{0.5em}{}[]
\titlespacing{\section}{0pc}{1pc}{1pc}

\titleformat{\subsection}[drop]{\large}{\thesubsection}{0.0em}{}[]
\titlespacing{\subsection}{5pc}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

\begin{document}

\section{Purpose and Discussion}
\subsection{}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam dapibus est mauris, varius malesuada velit consectetur quis. Phasellus bibendum iaculis maximus. Sed at ante sed dolor tempor viverra et eu lectus. Aliquam gravida nulla non nisi dapibus, quis dapibus magna fringilla. Vestibulum non gravida magna. Sed rutrum commodo odio sagittis condimentum. 

\subsection{definitions}    

\end{document}

我试图让它看起来像这样。 在此处输入图片描述

感谢您提供的任何帮助。

答案1

像这样吗?

在此处输入图片描述

我已经重新定义了\subsection,以便有\hangindent

\let\oldsubsection\subsection
\renewcommand\subsection[2][]{%
\oldsubsection[#1]{#2}%
\hangindent=2.7em%
}

runin用作形状选项\titleformat

\titleformat{\subsection}[runin]{\large}{\thesubsection}{0.5em}{}[]
\titlespacing{\subsection}{0.5em}{1em}{0.6em}

完整代码:

\documentclass[10pt]{article}

\usepackage{titlesec}

\titleformat{\section}[hang]{\large}{\thesection.0}{0.5em}{}[]
\titlespacing{\section}{0em}{1em}{0.6em}

\titleformat{\subsection}[runin]{\large}{\thesubsection}{0.5em}{}[]
\titlespacing{\subsection}{0.5em}{1em}{0.6em}

\let\oldsubsection\subsection
\renewcommand\subsection[2][]{%
\oldsubsection[#1]{#2}
\hangindent=2.7em
}

\begin{document}

\section{Purpose and Discussion}
\subsection{}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam dapibus est mauris, varius malesuada velit consectetur quis. Phasellus bibendum iaculis maximus. Sed at ante sed dolor tempor viverra et eu lectus. Aliquam gravida nulla non nisi dapibus, quis dapibus magna fringilla. Vestibulum non gravida magna. Sed rutrum commodo odio sagittis condimentum.

\subsection{definitions}

\end{document} 

答案2

使用该wrap风格能达到您想要的效果吗?

\documentclass[10pt]{article}

\usepackage[explicit, calcwidth]{titlesec}

\titleformat{\section}[hang]{\large}{\thesection.0}{0.5em}{#1}[]
\titlespacing{\section}{0pc}{1pc}{1pc}

\titleformat{\subsection}[wrap]{\filright\large}{\thesubsection~}{0.em}{#1}[]
\titlespacing{\subsection}{5pc}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

\begin{document}

\section{Purpose and Discussion}
\subsection{}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam dapibus est mauris, varius malesuada velit consectetur quis. Phasellus bibendum iaculis maximus. Sed at ante sed dolor tempor viverra et eu lectus. Aliquam gravida nulla non nisi dapibus, quis dapibus magna fringilla. Vestibulum non gravida magna. Sed rutrum commodo odio sagittis condimentum.

\subsection{\mbox{definitions} and so}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam dapibus est mauris, varius malesuada velit consectetur quis. Phasellus bibendum iaculis maximus. Sed at ante sed dolor tempor viverra et eu lectus. Aliquam gravida nulla non nisi dapibus, quis dapibus magna fringilla. Vestibulum non gravida magna. Sed rutrum commodo odio sagittis condimentum.

\end{document} 

在此处输入图片描述

相关内容