換行符在使用 tikz 修改的節標題中不起作用

換行符在使用 tikz 修改的節標題中不起作用

我對長子/節標題有疑問:\\無法正常工作。

由於我是 Latex 新手,所以我採用了這個漂亮的模板http://www.flortools.com/misc/LaTeXtemplate並根據我的需要進行了修改。

但是,現在我在子/節標題中遇到了換行符問題。

這是一個 MWE:

\documentclass[a4paper,12pt,oneside]{scrbook}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[scaled]{berasans}
\usepackage[scaled]{beramono} 
 \usepackage[english]{babel}
\usepackage[left=3cm,right=3cm,top=2.5cm,bottom=3cm,includehead,headheight=13.6pt]{geometry}
\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{calc,decorations,decorations.pathmorphing,arrows,matrix,positioning,patterns}
\usepackage{xcolor}
\selectcolormodel{cmyk}
 \newcommand\boxedSection[3]{{%
%
     \begin{tikzpicture}[inner sep=#3,line width=1.0pt]
         \node[anchor=east,rectangle,draw] at (0,0) (counter) {\textbf{#2}};
             \draw (counter.south west)  ++(.0pt,.5pt)-- ++($(\linewidth,0) - (2.5pt,0)$);
\node [right of=counter,anchor=west]{#1};
     \end{tikzpicture}
 }}
 \newcommand\boxedSectionB[3]{{%
%
     \begin{tikzpicture}[inner sep=#3,line width=1pt]
         \node[anchor=east,rectangle,draw,fill=black] at (0,0) (counter) {\color{white}\textbf{#2}};
             \draw (counter.south west) ++(.0pt,.5pt)-- ++($(\linewidth,0) - (2.5pt,0)$);
\node [right of=counter,anchor=west]{#1};
     \end{tikzpicture}
 }}
\newcommand\boxedsection[1]{\boxedSectionB{#1}{\thesection}{2mm}}
\newcommand\boxedsubsection[1]{\boxedSection{#1}{\thesubsection}{1.7mm}}
\newcommand\boxedsubsubsection[1]{\boxedSection{#1}{\thesubsubsection}{1.5mm}}
 \titleformat{\section}[hang]%
     {\usekomafont{section}}%
     {}%
     {.0em}%
     {\filright\boxedsection}%

 \titleformat{\subsection}[hang]%
     {\usekomafont{subsection}}%
     {}%
     {.0em}%
     {\filright\boxedsubsection}%

 \titleformat{\subsubsection}[hang]%
     {\usekomafont{subsubsection}}%
     {}%
     {.0em}%
     {\filright\boxedsubsubsection}%

\titlespacing{\section}{0pt}{1.5cm}{-1cm}
\titlespacing{\subsection}{0pt}{1.5cm}{-1cm}
\titlespacing{\subsubsection}{0pt}{6pt}{6pt}
\titlespacing{\paragraph}{}{0pt}{0pt}


\begin{document}

\subsection{This title is really really really long so I'd liked to have a \\line break but instead is just go on and on ...}

\end{document}

一旦我刪除了 titleformat 中的 boxedSection 部分,換行符就會\\再次起作用,但當然,漂亮的佈局也被刪除了。

有人可以修改 MWE 以便換行符號再次起作用,但仍保留良好的佈局。

多謝!

米施科

編輯 所以我找到了一個非常適合我的目的的解決方案。正如 touhami 建議的那樣,我在 boxedSection 中添加text width=13cmanchor=west 刪除了該行,同時保持 boxedSectionB 不變。所以程式碼看起來像這樣:

     \newcommand\boxedSection[3]{{%
%
     \begin{tikzpicture}[inner sep=#3,line width=1.0pt]
         \node[anchor=east,rectangle,draw] at (0,0) (counter) {\textbf{#2}};   
\node [right of=counter,anchor=west,text width=13cm]{#1};
     \end{tikzpicture}
 }}

結果如下圖所示: 在此輸入影像描述

答案1

更新

這是另一個不帶 package 的建議titlesec。現在標題下面沒有規則了。

\documentclass[a4paper,12pt,oneside]{scrbook}[2015/10/03]

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[scaled]{berasans}
\usepackage[scaled]{beramono} 
 \usepackage[english]{babel}
\usepackage[margin=3cm,top=2.5cm,includehead,headheight=13.6pt]{geometry}
\usepackage{xcolor}

\setcounter{secnumdepth}{\subsubsectionnumdepth}% subsubsection numbered

\RedeclareSectionCommands[
  font=\normalsize,
  %indent=-1em
]{section,subsection,subsubsection}

\newcommand*\BoxedSectionNumber[2]{{%
  \setlength\fboxrule{1pt}%
  \setlength\fboxsep{#2}%
  \ifstr{#1}{section}{%
    \colorbox{darkgray}{\textcolor{white}{\csname the#1\endcsname}}%
  }{\fbox{\csname the#1\endcsname}}%
  \hspace*{2\fboxsep}%
}}

\renewcommand*\sectionformat{\BoxedSectionNumber{section}{2mm}}
\renewcommand*\subsectionformat{\BoxedSectionNumber{subsection}{1.7mm}}
\renewcommand*\subsubsectionformat{\BoxedSectionNumber{subsection}{1.5mm}}

\newsavebox\SectionNumberBox

\renewcommand*\sectionlinesformat[4]{%
  \sbox{\SectionNumberBox}{\hspace*{#2}#3}%
  \noindent\usebox\SectionNumberBox%
  \parbox{\dimexpr\textwidth-\wd\SectionNumberBox\relax}{#4}%
  \par\nobreak%
}

\usepackage{blindtext}% dummy text

\begin{document}
\section{Section title}
\blindtext

\subsection{This title is really really really long so I'd liked
      to have a line break but instead is just go on and on ...}
\blindtext
\subsubsection{Subsubsection title}
\blindtext
\end{document}

結果:

在此輸入影像描述

如果取消註釋,indent=-1em您將得到

在此輸入影像描述


原答案

titlesec請注意,不建議將套件與 KOMA-Script 類別一起使用。您將收到 KOMA-Script 和titlesec.

這裡有一個建議titlesec,但tikz至少需要 KOMA-Script 版本 3.19a(目前在 CTAN 上)。

\documentclass[a4paper,12pt,oneside]{scrbook}[2015/10/03]

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[scaled]{berasans}
\usepackage[scaled]{beramono} 
 \usepackage[english]{babel}
\usepackage[left=3cm,right=3cm,top=2.5cm,bottom=3cm,includehead,headheight=13.6pt]{geometry}
\usepackage{xcolor}

\setcounter{secnumdepth}{\subsubsectionnumdepth}% subsubsection numbered

\RedeclareSectionCommands[font=\normalfont]{section,subsection,subsubsection}
\RedeclareSectionCommands[
  beforeskip=-1.5cm,
  afterskip=1cm,
  font=\normalfont
]{section,subsection}
\RedeclareSectionCommand[
  beforeskip=-12pt,
  afterskip=6pt,
  font=\normalfont
]{subsubsection}

\newcommand*\sectionnumberfboxsep{2mm}
\newcommand*\subsectionnumberfboxsep{1.7mm}
\newcommand*\subsubsectionnumberfboxsep{1.5mm}

\renewcommand*\sectionformat{{%
  \setlength\fboxsep{\sectionnumberfboxsep}%
  \setlength\fboxrule{1pt}%
  \colorbox{black}{\bfseries\textcolor{white}{\thesection}}%
}}
\renewcommand*\subsectionformat{{%
  \setlength\fboxsep{\subsectionnumberfboxsep}%
  \setlength\fboxrule{1pt}%
  \fbox{\bfseries\thesubsection}%
}}
\renewcommand*\subsubsectionformat{{%
  \setlength\fboxsep{\subsubsectionnumberfboxsep}%
  \setlength\fboxrule{1pt}%
  \fbox{\bfseries\thesubsubsection}%
}}
\makeatletter
\renewcommand*\sectionlinesformat[4]{%
  \ifstr{#1}{section}{%
    \leavevmode\rlap{\rule[\dimexpr-\sectionnumberfboxsep-1pt\relax]{\textwidth}{1pt}}%
    \parbox[b]{3em}{#3}\parbox[b]{\dimexpr\textwidth-3em}{#4}\par\nobreak%
  }{%
  \ifstr{#1}{subsection}{%
    \leavevmode\rlap{\rule[\dimexpr-\subsectionnumberfboxsep-1pt\relax]{\textwidth}{1pt}}%
    \parbox[b]{4em}{#3}\parbox[b]{\dimexpr\textwidth-4em}{#4}\par\nobreak%
  }{%
  \ifstr{#1}{subsubsection}{%
    \leavevmode\rlap{\rule[\dimexpr-\subsubsectionnumberfboxsep-1pt\relax]{\textwidth}{1pt}}%
    \parbox[b]{4.75em}{#3}\parbox[b]{\dimexpr\textwidth-4.75em}{#4}\par\nobreak%
  }{%
    \@hangfrom{\hspace*{#2}#3}{#4}%
  }}}%
}
\makeatletter

\usepackage{blindtext}% dummy text

\begin{document}
\section{Section title}
\blindtext
\subsection{This title is really really really long so I'd liked to have a line break but instead is just go on and on ...}
\blindtext
\subsubsection{Subsubsection title}
\blindtext
\end{document}

在此輸入影像描述

相關內容