我一直在想“如何只为章节编号获取阴影框”?此外,如何在页眉中的左标记和页码之间获取灰色方块?在下面的代码中,我可以得到纯黑色方块。我已在下面附上所需输出的图像。此示例来自 TUGINDIA 教程。
\documentclass[12pt,twoside]{report}
\usepackage{titlesec}%for formatting chapter position and names
\titleformat{\chapter}[display]
{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{0pt}{\huge}
\titlespacing*{\chapter}{0pt}{-20pt}{10pt}
\title{\bf \huge xxx}
\author{xxx}
\date{} %to disable date
\usepackage[top=1.3in, left=1.5in, right=1in]{geometry} %for setting margins
\usepackage{grffile} %to avoid showing path of figure inserted
\includeonly{chap1/chapter_1}
\titlelabel{\llap{\thetitle\quad}}% for section headings in margins
\usepackage{fancyhdr}
\pagestyle{fancy}
\newcommand{\helv}{%
\fontfamily{phv}\fontseries{}\fontsize{10}{12}\selectfont}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}%formatting chapter title
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\fancyhf{}
\fancyhead[RO]{\helv \rightmark{} $\blacksquare$ \thepage}
\fancyhead[LE]{\helv \thepage{} $\blacksquare$ \leftmark}
\renewcommand{\headrulewidth}{0pt}
\usepackage{amssymb}
\parindent=0.5in
\parskip=10pt
\linespread{1.3}
\renewcommand{\abstractname}{\Large Abstract} %To increase Abstract font size
\begin{document}
\maketitle
\begin{abstract}
blahblah
\end{abstract}
blah blah
\end{document}
答案1
这是让 Section 标题带有颜色的方法。但是,我没有花时间让边距看起来像示例一样,因为您的问题似乎集中在框和颜色方面,而不是边距方面。
另外,我只将效果实现到了小节级别,但是如果您愿意的话,可以按照我的做法将其扩展到小节级别。
至于您对页码旁边的灰色方块分隔符的问题,类似的事情在这里讨论:页边距和分隔符中的页码
\documentclass{article}
\usepackage{color}
\makeatletter
\definecolor{sectcolor}{rgb}{0.9,0.9,0.9}
\let\origsection\section
\let\origsubsection\subsection
\def\thesectiontext{\@arabic\c@section.}
\def\thesubsectiontext{\thesectiontext\@arabic\c@subsection}
\global\newsavebox{\sectbox}
\def\makesectbox#1{\savebox\sectbox{\colorbox{sectcolor}
{\makebox[0.5in][r]{\normalfont\large\bfseries#1}}}}
\renewcommand \thesection {\usebox{\sectbox}}
\renewcommand \thesubsection {\usebox{\sectbox}}
\renewcommand\section{\@ifstar{\myheading}{\mysection}}
\newcommand\mysection[1]{%
\sectprelude{section}{\thesectiontext}\origsection{#1}}
\newcommand\myheading[1]{%
\sectprelude{section}{\thesectiontext}\origsection*{#1}}
\renewcommand\subsection[1]{%
\sectprelude{subsection}{\thesubsectiontext}\origsubsection{#1}}
\newcommand\sectprelude[2]{%
\addtocounter{#1}{1}\makesectbox{#2}\addtocounter{#1}{-1}}
\makeatother
\begin{document}
\section{Introduction}
It was a dark and stormy night\ldots and then, a minute passed.
\section{Discussion}
In the beginning\ldots
\section{Conclusion}
\ldots and it was good
\subsection{Postscript}
Amen.
\end{document}
答案2
这是一个使用titlesec
包装并制作彩色盒子和所需的悬挂;下面的示例还包括生成文档所需标题所需的设置:
\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{fancyhdr}
\colorlet{sectcolor}{gray!65}
\newcommand\graysquare{\textcolor{sectcolor}{\rule{1ex}{1ex}}}
\newcommand{\helv}{\fontfamily{phv}\fontseries{}\fontsize{10}{12}\selectfont}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\fancyhf{}
\fancyhead[RO]{\helv\rightmark\hspace{0.5em}\graysquare\hspace{0.5em}\thepage}
\fancyhead[LE]{\helv\thepage\hspace{0.5em}\graysquare\hspace{0.5em}\leftmark}
\renewcommand{\headrulewidth}{0pt}
\titleformat{\section}
{\normalfont\Large\bfseries}
{\llap{\colorbox{sectcolor}{\makebox[3em][r] {\thesection}}\hspace{1em}}}
{0em}{#1}
\begin{document}
\setcounter{chapter}{1}% just for the example
\section{Introduction}
A cross-reference to the last section:~\ref{sec:conclusion}.
\lipsum[4]
\section{Discussion}
\lipsum[4]
\section{Conclusion}
\label{sec:conclusion}
\lipsum[4]
\end{document}
结果图像:
我的解决方案仅改变文档正文中章节编号的格式;目录条目中的交叉引用和编号将保留标准表示。
答案3
如果你使用KOMA-class
您不需要任何其他软件包:
\documentclass{scrbook}
\usepackage{xcolor}
\usepackage{lipsum}
\definecolor{sectcolor}{rgb}{0.9,0.9,0.9}
\renewcommand*{\othersectionlevelsformat}[3]{%
\llap{\colorbox{sectcolor}{\makebox[2.5em][r]{#3\autodot}}\quad}%
}
\begin{document}
\chapter{Test Chapter}
\section{Introduction}
A cross-reference to the last section:~\ref{sec:conclusion}.
\lipsum[4]
\section{Discussion}
\lipsum[4]
\section{Conclusion}
\label{sec:conclusion}
\lipsum[4]
\end{document}
答案4
你只需改变\@seccntformat
例如,
\def\@seccntformat#1{\colorbox{grayten}{\csname the#1\endcsname\quad}}