如何将下面代码中的长标题分成几行?我试过了\\
,但\newline
没有成功。
\documentclass[12pt]{extbook}
\usepackage{extsizes}
\usepackage{pgfornament,adjustbox}
\usepackage{minitoc}
\usepackage{titletoc}
\usepackage[explicit,calcwidth]{titlesec}
\renewcommand{\theparagraph}{\arabic{paragraph}.\;}
%---------------------------------------------------
\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{5}
%---------------------------------------------------
\newcommand{\fancydraw}{% Defining a command to shorten things
\begin{adjustbox}{max height=0.5\baselineskip}% Rescaling to have height of 0.5\baselineskip
\rotatebox{90}{% And rotating 90 degrees
\pgfornament{26}% Ornament n° 26 (http://melusine.eu.org/syracuse/pstricks/véc-tơian/psvéc-tơian.pdf)
}%
\end{adjustbox}%
}
\titlespacing{\paragraph}{-3.2pt}{8pt}{8pt}
\titleformat{\paragraph}
{\footnotesize\bfseries\fontfamily{qag}\selectfont}
{}
{0.2em}
{\centering% Centering the title
\tikz[baseline=(todotted.base)]{
\node[inner sep=1pt,outer sep=0pt] (todotted) {\fancydraw \hspace{1em} \theparagraph #1 \hspace{1em} \rotatebox[origin=c]{180}{\fancydraw}};
\draw[line width=.6pt] ([yshift=16pt]todotted.south west) -- ([yshift=16pt]todotted.south east); % 5 pt above
\draw[line width=.6pt] ([yshift=-3pt]todotted.south west) -- ([yshift=-3pt]todotted.south east); % 5 pt below
% \draw ([yshift=-7pt]todotted.south west) -- ([yshift=-7pt]todotted.south east); % and 7 pt below
}
}[]
\begin{document}
\paragraph{This is long title This is long title This is long title This is long title This is long title This is long title This is long title This is long title This is long title This is long title}
\end{document}
答案1
您正在使用 TikZ 生成\paragraph
标题。要自动允许 TikZ 中的换行符\node
,您只需设置允许的text width
。对于多行节点,也不todotted.south
适合使用上述行作为参考点。最好使用todotted.north
。因此,您可以例如更改
\node[inner sep=1pt,outer sep=0pt] (todotted) {\fancydraw \hspace{1em} \theparagraph #1 \hspace{1em} \rotatebox[origin=c]{180}{\fancydraw}};
\draw[line width=.6pt] ([yshift=16pt]todotted.south west) -- ([yshift=16pt]todotted.south east); % 5 pt above
进入
\node[inner sep=1pt,outer sep=0pt,text width=\linewidth] (todotted) {\fancydraw \hspace{1em} \theparagraph #1 \hspace{1em} \rotatebox[origin=c]{180}{\fancydraw}};
\draw[line width=.6pt] ([yshift=5pt]todotted.north west) -- ([yshift=5pt]todotted.north east); % 5 pt above
有关节点多行文本的更多格式化选项,请参阅 TikZ 手册中的“17.4 节点文本”一节,即“17.4.3 文本参数:多行文本的对齐和宽度”。例如,您可以添加align=center
标题的水平居中:
\documentclass[12pt]{extbook}
%\usepackage{extsizes}% DOES NOT MAKE SENSE WITH CLASS extbook
\usepackage{pgfornament,adjustbox}
\usepackage{minitoc}
\usepackage{titletoc}
\usepackage[explicit,calcwidth]{titlesec}
\renewcommand{\theparagraph}{\arabic{paragraph}.\;}
%---------------------------------------------------
\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{5}
%---------------------------------------------------
\newcommand{\fancydraw}{% Defining a command to shorten things
\begin{adjustbox}{max height=0.5\baselineskip}% Rescaling to have height of 0.5\baselineskip
\rotatebox{90}{% And rotating 90 degrees
\pgfornament{26}% Ornament n° 26 (http://melusine.eu.org/syracuse/pstricks/véc-tơian/psvéc-tơian.pdf)
}%
\end{adjustbox}%
}
\titlespacing{\paragraph}{-3.2pt}{8pt}{8pt}
\titleformat{\paragraph}
{\footnotesize\bfseries\fontfamily{qag}\selectfont}
{}
{0.2em}
{\centering% Centering the title
\tikz[baseline=(todotted.base)]{
\node[inner sep=1pt,outer sep=0pt,text width=\linewidth,align=center] (todotted) {\fancydraw \hspace{1em} \theparagraph #1 \hspace{1em} \rotatebox[origin=c]{180}{\fancydraw}};
\draw[line width=.6pt] ([yshift=5pt]todotted.north west) -- ([yshift=5pt]todotted.north east); % 5 pt above
\draw[line width=.6pt] ([yshift=-3pt]todotted.south west) -- ([yshift=-3pt]todotted.south east); % 5 pt below
% \draw ([yshift=-7pt]todotted.south west) -- ([yshift=-7pt]todotted.south east); % and 7 pt below
}
}[]
\begin{document}
\paragraph{This is long title This is long title This is long title This is long title This is long title This is long title This is long title This is long title This is long title This is long title}
\end{document}
如果您不需要自动换行符,而\\
只需要手动使用,您也可以使用node halign header
。这会将行的长度减少到行的自然最大值。在下面的例子中,我使用了居中标题的定义:
\documentclass[12pt]{extbook}
%\usepackage{extsizes}% DOES NOT MAKE SENSE WITH CLASS extbook
\usepackage{pgfornament,adjustbox}
\usepackage{minitoc}
\usepackage{titletoc}
\usepackage[explicit,calcwidth]{titlesec}
\renewcommand{\theparagraph}{\arabic{paragraph}.\;}
%---------------------------------------------------
\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{5}
%---------------------------------------------------
\newcommand{\fancydraw}{% Defining a command to shorten things
\begin{adjustbox}{max height=0.5\baselineskip}% Rescaling to have height of 0.5\baselineskip
\rotatebox{90}{% And rotating 90 degrees
\pgfornament{26}% Ornament n° 26 (http://melusine.eu.org/syracuse/pstricks/véc-tơian/psvéc-tơian.pdf)
}%
\end{adjustbox}%
}
\titlespacing{\paragraph}{-3.2pt}{8pt}{8pt}
\def\paragraphheader{\hfil##\hfil\cr}% Used in the TikZ node of following \titleformat
\titleformat{\paragraph}
{\footnotesize\bfseries\fontfamily{qag}\selectfont}
{}
{0.2em}
{\centering% Centering the title
\tikz[baseline=(todotted.base)]{
\node[inner sep=1pt,outer sep=0pt,node halign header=\paragraphheader] (todotted) {\fancydraw \hspace{1em} \theparagraph #1 \hspace{1em} \rotatebox[origin=c]{180}{\fancydraw}};
\draw[line width=.6pt] ([yshift=5pt]todotted.north west) -- ([yshift=5pt]todotted.north east); % 5 pt above
\draw[line width=.6pt] ([yshift=-3pt]todotted.south west) -- ([yshift=-3pt]todotted.south east); % 5 pt below
% \draw ([yshift=-7pt]todotted.south west) -- ([yshift=-7pt]todotted.south east); % and 7 pt below
}
}[]
\begin{document}
\paragraph{This is long title This is long title\\
This is long title This is long title\\ This is long title This is long title\\ This is long title This is long title\\ This is long title This is long title}
\end{document}
查看与第一个例子的图像的区别。