LaTeX/主題
LaTeX 新手經常會因為該系統缺乏視覺自定義功能而感到失望。實際上,這是有意為之:LaTeX 的理念是專注于格式化,而讓作者專注於內容。
在本節中,我們將展示透過一些努力可以實現的目標。
簡介
[edit | edit source]接下來,我們將編寫主題,一個只改變文件外觀的包,這樣我們的文件就可以使用或不使用主題。
請注意,雖然它可能看起來很美觀,但這絕對不是排版的典範。你不應該在嚴肅的出版物中使用這種主題。這更多的是一個技術示例,展示 LaTeX 的功能。
-
自定義主題(目錄)
-
自定義主題
-
自定義主題(紅色)
包配置
[edit | edit source]這裡沒有什麼好說的。這直接應用了建立包一章的內容。
我們載入所需的包。
- needspace 用於防止在分節命令後立即出現分頁符。
- tikz 用於繪製花哨的素材。
我們定義了一個顏色選項,你可以根據需要使用任意數量的選項。使用特定名稱定義顏色使其非常靈活。我們還使用了一個選項來切換花哨的反射效果,這可能有點過分!
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{theme-fancy}[2013/01/13 v1.0 fancy theme]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Packages
\RequirePackage{geometry}
\RequirePackage{needspace}
\RequirePackage[svgnames]{xcolor}
\RequirePackage{hyperref}
\hypersetup{colorlinks=true}
\RequirePackage{fancyhdr}
\RequirePackage{tikz}
\usetikzlibrary{
calc,
decorations.pathmorphing,
fadings,
shadows,
shapes.geometric,
shapes.misc,
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Options
%% Toggle reflection.
\newif\if@mirrors\@mirrorsfalse
\DeclareOption{mirrors}{
\@mirrorstrue
}
%% Colors.
\newif\if@red\@redfalse
\DeclareOption{red}{
\@redtrue
}
\ExecuteOptions{}
\ProcessOptions\relax
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Configuration
\renewcommand{\familydefault}{\sfdefault}
\setlength{\parskip}{0.5\baselineskip}
%% Colors
\colorlet{toctitle}{DarkGray!50!black}
\colorlet{titlebg}{MidnightBlue}
\colorlet{titlefg}{LightBlue}
\colorlet{titletxt}{MidnightBlue}
\colorlet{sectionfg}{MidnightBlue}
\colorlet{subsectionfg}{SteelBlue}
\colorlet{subsubsectionfg}{LightSteelBlue!60!black}
\if@red
\colorlet{toctitle}{DarkGray!50!black}
\colorlet{titlebg}{DarkRed}
\colorlet{titlefg}{FireBrick!50}
\colorlet{titletxt}{DarkRed}
\colorlet{sectionfg}{DarkRed}
\colorlet{subsectionfg}{Crimson!50!black}
\colorlet{subsubsectionfg}{LightSteelBlue!60!black}
\fi
|
頁首和頁尾
[edit | edit source]我們使用 TikZ 繪製一個填充的半圓。
fancyhdr 用於設定頁首和頁尾。我們注意使用 fancy 樣式,並透過 \fancyhf{} 從頭開始擦除之前的頁首和頁尾。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Header and Footer
\tikzstyle{foliostyle}=[fill=Lavender, text=MidnightBlue, inner sep=5pt, semicircle]
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[C]{
\vskip 3pt
\begin{tikzpicture}
\node[foliostyle]
{\bfseries\thepage};
\end{tikzpicture}
}
\renewcommand{\headrulewidth}{0.8pt}
\addtolength{\headheight}{\baselineskip}
\renewcommand{\headrule}{\color{LightGray}\hrule}
\fancyhead[LE]{ \textcolor{gray}{\slshape \rightmark} }
\fancyhead[RO]{ \textcolor{gray}{\slshape \leftmark} }
|
目錄
[edit | edit source]我們重新定義了 \tableofcontents 所使用的命令。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Table of contents
\newcommand{\sectiontoccolor}{sectionfg}
\newcommand{\subsectiontoccolor}{subsectionfg}
\newcommand{\subsubsectiontoccolor}{subsubsectionfg}
\renewcommand*\l@section{\color{\sectiontoccolor}\def\@linkcolor{\sectiontoccolor}\@dottedtocline{1}{1.5em}{2.3em}}
\renewcommand*\l@subsection{\color{\subsectiontoccolor}\def\@linkcolor{\subsectiontoccolor}\@dottedtocline{1}{2.3em}{3.1em}}
\renewcommand*\l@subsubsection{\color{\subsubsectiontoccolor}\def\@linkcolor{\subsubsectiontoccolor}\@dottedtocline{1}{3.1em}{3.9em}}
\def\contentsline#1#2#3#4{%
\ifx\\#4\\%
\csname l@#1\endcsname{#2}{#3}%
\else
\csname l@#1\endcsname{\hyper@linkstart{link}{#4}{#2}\hyper@linkend}{%
\hyper@linkstart{link}{#4}{#3}\hyper@linkend
}%
\fi
}
%% New title format -- 'section' is used by default.
\newcommand{\tocformat}[1]{{\Huge\bf#1}}
\renewcommand\tableofcontents{%
\tocformat{
\textcolor{toctitle}{\contentsname}
\@mkboth{\MakeUppercase\contentsname}{\MakeUppercase\contentsname}
}%
\@starttoc{toc}%
}
|
分節
[edit | edit source]這絕對是最複雜的部分。它並不難,因為 \section、\subsection 和 \subsubsection 的程式碼幾乎相同。
我們使用 \needspace 來確保在分節命令後不會出現換行符。我們將命令包含在一個組中,在該組中設定字型大小,因為我們需要的是 \baselineskip,它取決於字型大小。
帶星號的命令不會設定計數器(LaTeX 的預設行為)。你可以選擇透過重置計數器來以不同的方式處理帶星號的命令。
我們在列印部分之前使用 \noindent。我們確保使用 \par 命令結束列印部分,以確保後面的文字能夠正確列印。
對於 \subsection,我們使用 mirrors 選項來相應地更改外觀。
為了正確處理 PDF 書籤,我們需要在定義的末尾新增以下幾行。
\phantomsection
\addcontentsline{toc}{section}{\thesection~#1}
|
最後,僅對於 \section,我們希望它在頁首中列印,因此我們呼叫 \sectionmark 命令。在這裡,我們改變了帶星號命令的行為,使其與原始 LaTeX 版本不同,因為我們定義並使用了 \sectionmarkstar 命令。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Section style
\renewcommand\section{
\@ifstar
\my@sectionstar
\my@section
}
%% Note: to justify, text width must be set to \textwidth - 2*(inner sep).
\tikzstyle{sectionstyle}=[
inner sep=5pt,
text width=\textwidth-10pt,
left color=sectionfg!100!white,
right color=sectionfg!50!white,
rounded corners,
text=Ivory,
rectangle
]
\newcommand\my@section[1]{
\stepcounter{section}
{\Large\needspace{\baselineskip}}
\noindent
\begin{tikzpicture}
\node[sectionstyle] {\bfseries\Large\thesection\quad#1};
\end{tikzpicture}
\par
\phantomsection
\addcontentsline{toc}{section}{\thesection~#1}
\sectionmark{#1}
}
\newcommand{\sectionmarkstar}[1]{\markboth{\MakeUppercase{#1}}{}}
\newcommand\my@sectionstar[1]{
{\Large\needspace{\baselineskip}}
\noindent
\begin{tikzpicture}
\node[sectionstyle] {\bfseries\Large#1};
\end{tikzpicture}
\par
\phantomsection
\addcontentsline{toc}{section}{#1}
\sectionmarkstar{#1}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Subsection style
\renewcommand\subsection{
\@ifstar
\my@subsectionstar
\my@subsection
}
\tikzstyle{subsectionstyle}=[
left color=subsectionfg!50!white,
right color=subsectionfg!100!white,
text=Ivory,
ellipse,
inner sep=5pt
]
\newcommand\my@subsection[1]{
\stepcounter{subsection}
{\Large\needspace{\baselineskip}}
\noindent
\begin{tikzpicture}
\node[subsectionstyle,anchor=west] (number) at (0,0) {\bfseries\Large\thesubsection};
\if@mirrors
\node[above right,subsectionfg,anchor=south west] at ($(number.east)+(0.1,-0.1)$) {\large\bfseries#1};
\node[yscale=-1, scope fading=south, opacity=0.4, above, anchor=south west, subsectionfg] at ($(number.east)+(0.1,0.1)$) {\large\bfseries#1};
\else
\node[above right,subsectionfg,anchor=west] at ($(number.east)+(0.1,0)$) {\large\bfseries#1};
\fi
\end{tikzpicture}
\par
\phantomsection
\addcontentsline{toc}{subsection}{\thesubsection~#1}
}
\newcommand\my@subsectionstar[1]{
{\Large\needspace{\baselineskip}}
\noindent
\begin{tikzpicture}
\node[subsectionstyle,anchor=west] (number) at (0,0) {\bfseries\Large\phantom{1}};
%
\if@mirrors
\node[above right,subsectionfg,anchor=south west] at ($(number.east)+(0.1,-0.1)$) {\large\bfseries#1};
\node[yscale=-1, scope fading=south, opacity=0.4, above, anchor=south west, subsectionfg] at ($(number.east)+(0.1,0.1)$) {\large\bfseries#1};
\else
\node[above right,subsectionfg,anchor=west] at ($(number.east)+(0.1,0)$) {\large\bfseries#1};
\fi
\end{tikzpicture}
\par
\phantomsection
\addcontentsline{toc}{subsection}{#1}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Subsubsection style
\renewcommand\subsubsection{
\@ifstar
\my@subsubsectionstar
\my@subsubsection
}
\tikzstyle{subsubsectionstyle}=[
left color=subsubsectionfg!50!white,
right color=subsubsectionfg!100!white,
text=Ivory,
shape=trapezium,
inner sep=5pt
]
\newcommand\my@subsubsection[1]{
\stepcounter{subsubsection}
\noindent
\begin{tikzpicture}
\node[subsubsectionstyle] (number) {\bfseries\large\thesubsubsection};
\node[subsubsectionfg, right of=number, anchor=west] {\large\bfseries#1};
\end{tikzpicture}
\par
\phantomsection
\addcontentsline{toc}{subsubsection}{\thesubsubsection~#1}
}
\newcommand\my@subsubsectionstar[1]{
\noindent
\begin{tikzpicture}
\node[subsubsectionstyle] (number) {\bfseries\large\vphantom{1}};
\node[subsubsectionfg, right of=number, anchor=west] {\large\bfseries#1};
\end{tikzpicture}
\par
\phantomsection
\addcontentsline{toc}{subsubsection}{#1}
}
\endinput
|
註釋和參考文獻
[edit | edit source]
