更改標題顏色

更改標題顏色

我成功地將部分和小節的顏色改為我定義的顏色。不過我想用\title同樣的方式改變它,但\titlefont{•}不起作用(我想它不應該這樣做)。是什麼簡單的\title還有改變顏色的方法嗎?我有簡化的以下程式碼:

\documentclass[11pt,a4paper]{article}
\usepackage{amsmath, url}
\usepackage[utf8x]{inputenc}
\usepackage[czech]{babel}
\usepackage{xcolor}
\usepackage{sectsty}

\definecolor{astral}{RGB}{46,116,181}
\subsectionfont{\color{astral}}
\sectionfont{\color{astral}}

\title{...}
\author{...}

\begin{document}
    \maketitle    
    \begin{abstract} ...
    \end{abstract}    
    \smallskip
    \noindent \textbf{Key words:} ...   
    \section{...}
\end{document}

我們非常歡迎每一個幫助及其描述。

答案1

直接將顏色插入\title

在此輸入影像描述

\documentclass{article}

\usepackage{lipsum}% Just for this example
\usepackage{xcolor,sectsty}

\definecolor{astral}{RGB}{46,116,181}
\subsectionfont{\color{astral}}
\sectionfont{\color{astral}}

\title{\color{astral} My title}
\author{An Author}

\begin{document}

\maketitle

\begin{abstract}
  \lipsum[1]
\end{abstract}

\smallskip

\noindent \textbf{Summary:}
\lipsum*[2]

\section{A section}
\lipsum[3]

\end{document}

上述建議可能看起來很粗糙。然而,它是作為一次性使用巨集的一部分插入的\title,因此對於該方法來說已經足夠了。對於更正式的方法,您可以修補內部\@maketitle巨集 - 負責設定標題顯示:

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@maketitle}% <cmd>
  {\@title}% <search>
  {\color{\@titlecolor}\@title}% <replace>
  {}{}% <success><failure>
\newcommand{\@titlecolor}{black}
\newcommand{\titlecolor}[1]{\renewcommand{\@titlecolor}{#1}}
\makeatother

上面的程式碼\titlecolor{<color>}可讓您根據需要切換到顏色(例如,\titlecolor{astral}將提供相同的輸出)。此程式碼也依賴 的結構\@maketitle,該結構可能與其他類別不同或受某些套件的影響。


\title解決標題顏色變更的另一種(不太正式的)方法可能是僅更新處理其參數的方式:

\makeatletter
\renewcommand{\title}[1]{\renewcommand{\@title}{\color{\@titlecolor}#1}}
\newcommand{\@titlecolor}{black}
\newcommand{\titlecolor}[1]{\renewcommand{\@titlecolor}{#1}}
\makeatother

答案2

避免在諸如\title.儘管這通常是文件中的一次性命令 - 如果只是因為\maketitle透過擦除所有內容來強制執行此命令 - 我認為最好還是避免它。

另一種選擇並不是對內部命令進行看起來可怕的修補。就像章節標題的情況一樣,使用一個包可以為你做一些可怕的事情。在這種情況下titling專門定製文件標題的格式。

例如:

\pretitle{\begin{center}\LARGE\color{astral}}
\posttitle{\par\end{center}\vskip 0.5em}

這比添加顏色稍微複雜一些,\title但也複雜不了多少。我從從titling的文檔中複製的預設值開始:

\pretitle{\begin{center}\LARGE}
\posttitle{\par\end{center}\vskip 0.5em}

然後,我只是在字體規範之後添加了顏色規範\LARGE

就是這樣:

自訂標題

\documentclass[11pt,a4paper]{article}
\usepackage{amsmath,url}
\usepackage[utf8x]{inputenc}
\usepackage[czech]{babel}
\usepackage{xcolor}
\usepackage{sectsty,titling}

\definecolor{astral}{RGB}{46,116,181}
\subsectionfont{\color{astral}}
\sectionfont{\color{astral}}

\pretitle{\begin{center}\LARGE\color{astral}}
\posttitle{\par\end{center}\vskip 0.5em}

\title{Title}
\author{Author}

\begin{document}
\maketitle
\begin{abstract}
  An abstract
\end{abstract}
\smallskip
\noindent \textbf{Klíčová slova:}\dots
\section{A section}
\end{document}

相關內容