使用此代码:
\RequirePackage{fix-cm}
\documentclass[fontsize=8.5pt]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{multicol}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{linegoal}
\titleformat{\section}
{\fontsize{12pt}{0}\bfseries}{}{0em}
{%
\begin{tikzpicture}
\node[inner ysep=1.75mm,text width=85mm,
align=left,left color=black!15,right color=black!15]
{\parbox{5mm}{\raggedleft\thesection}\hspace{5mm}\parbox[t][0pt][t]{\dimexpr\linewidth-15mm}{\raggedright #1}};
\end{tikzpicture}%
}
\begin{document}
\begin{multicols}{2}
\section{gang}
\section{hound}
\end{multicols}
\end{document}
部分名称和部分编号不知为何发生了位移(用红线显示)。我怎样才能将编号和名称放在同一条(红)线上?
答案1
不建议titlesec
与 KOMA-Script 类一起使用。因此,这里有一个不使用此包的建议:
\RequirePackage{fix-cm}
\documentclass[fontsize=8.5pt,DIV=calc]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{multicol}
\usepackage{xcolor}
\addtokomafont{section}{\Large}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\ifstr{#1}{section}
{\setlength\fboxsep{1.75mm}%
\colorbox{black!15}{\raisebox{0pt}[\height][0pt]{%
\parbox[b]{\dimexpr\linewidth-2\fboxsep\relax}{%
\raggedsection\@hangfrom{\hskip#2#3}{#4}%
}}}}
{\@hangfrom{\hskip#2#3}{#4}}%
}
\renewcommand\sectionformat{%
\makebox[10mm][r]{\thesection\autodot\hspace{5mm}}
}
\makeatother
\begin{document}
\begin{multicols}{2}
\section{gang}
\section{hound}
\section{section with a very very long title}
\end{multicols}
\end{document}
结果:
答案2
答案3
TikZ
当一个简单的\colorbox
意愿就能达到目的时,使用还有什么意义呢?
\RequirePackage{fix-cm}
\documentclass[fontsize=8.5pt]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{helvet}
\usepackage[table, svgnames]{xcolor}
\usepackage{tabularx} %
\renewcommand{\familydefault}{\sfdefault}
\usepackage{multicol}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{linegoal}
\titleformat{\section}
{\fontsize{12pt}{0}\bfseries}{}{0em}
{\setlength\fboxsep{1.75mm}\colorbox{Gainsboro!40!Lavender}{\makebox[5mm][r]{\thesection}\hspace{5mm}%
\parbox[t][0pt]{\dimexpr\linewidth-2\fboxsep-10mm\relax}{\raggedright#1}}%
}
\begin{document}
\begin{multicols}{2}
\section{gang}
\section{hound}
\end{multicols}
\end{document}