如何在 moderncv 中为完整标题加下划线?

如何在 moderncv 中为完整标题加下划线?

以下模板中每个部分的标题moderncv都带有下划线,\hrule宽度固定为 1.5cm:

\documentclass[11pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}    
\definecolor{color1}{rgb}{0,0,1}
\usepackage[UKenglish]{babel}
\usepackage[scale=0.8]{geometry}
\firstname{}
\familyname{}

\makeatletter
\renewcommand\sectionfont{\bfseries\large}
\renewcommand*{\section}[1]{%
\par
\phantomsection{}% reset the anchor for hyperrefs
\addcontentsline{toc}{section}{#1}
\sectionstyle{#1}
\strut%removes too low hrule when letter of section title extends into baseline
{\color{blue}\hrule width 1.5cm height 2pt}
\par\nobreak\addvspace{1ex}\@afterheading}
\makeatother

\begin{document}

\maketitle

\section{Education}
\section{Key Skills}
\section{Scholarships}

\end{document}

我该如何改变这种情况并为每个标题的整个标题加下划线?

答案1

这是一个解决方案:

\documentclass[11pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}    
\definecolor{color1}{rgb}{0,0,1}
\usepackage[UKenglish]{babel}
\usepackage[scale=0.8]{geometry}
\firstname{}
\familyname{}

\newlength{\mylength}
\newsavebox{\mybox}

\makeatletter
\renewcommand\sectionfont{\bfseries\large}
\renewcommand*{\section}[1]{%
\par
\phantomsection{}% reset the anchor for hyperrefs
\addcontentsline{toc}{section}{#1}
\sectionstyle{#1}
\strut%removes too low hrule when letter of section title extends into baseline
\savebox{\mybox}{\bfseries\large #1}
\settowidth{\mylength}{\usebox{\mybox}}
{\color{blue}\hrule width \mylength height 2pt}
\par\nobreak\addvspace{1ex}\@afterheading}
\makeatother


\begin{document}

\maketitle

\section{Education}
\section{Key Skills}
\section{Scholarships}

\end{document}

我认为您实际上并不需要 savebox 来实现这一点,而且可以更轻松地完成...您可以按照自己的方式通过计算部分宽度来完成。

答案2

也许是这个(正如我的评论所建议的那样)?

\documentclass[11pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}    
\definecolor{color1}{rgb}{0,0,1}
\usepackage[UKenglish]{babel}
\usepackage[scale=0.8]{geometry}
\firstname{}
\familyname{}

\makeatletter
\renewcommand\sectionfont{\bfseries\large}
\renewcommand*{\section}[1]{%
\par
\phantomsection{}% reset the anchor for hyperrefs
\addcontentsline{toc}{section}{#1}
\sectionstyle{\underline{#1\rule[-2pt]{0pt}{0pt}}}
\par\nobreak\addvspace{1ex}\@afterheading}
\makeatother

\begin{document}

\maketitle

\section{Education}
\section{Key Skills}
\section{Scholarships}

\end{document}

在此处输入图片描述

答案3

下面打印的是与章节标题一样宽的规则:

\documentclass[11pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}    
\definecolor{color1}{rgb}{0,0,1}
\usepackage[UKenglish]{babel}
\usepackage[scale=0.8]{geometry}
\firstname{}
\familyname{}

\makeatletter
\renewcommand\sectionfont{\bfseries\large}
\renewcommand*{\section}[1]{%
\par
\phantomsection{}% reset the anchor for hyperrefs
\addcontentsline{toc}{section}{#1}
\setbox0\hbox{\sectionstyle{#1}}
\usebox0
\strut%removes too low hrule when letter of section title extends into baseline
{\color{blue}\hrule width \wd0 height 2pt}
\par\nobreak\addvspace{1ex}\@afterheading}
\makeatother

\begin{document}

\maketitle

\section{Education}
\section{Key Skills}
\section{Scholarships}

\end{document}

在此处输入图片描述

利用该包的另一种可能性ulem

\documentclass[11pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}    
\definecolor{color1}{rgb}{0,0,1}
\usepackage[normalem]{ulem}
\usepackage[UKenglish]{babel}
\usepackage[scale=0.8]{geometry}
\firstname{}
\familyname{}

\newcommand*{\cusuline}{%
    \bgroup\markoverwith{\rule[-1ex]{0.25ex}{2pt}}\ULon}%

\makeatletter
\renewcommand\sectionfont{\bfseries\large}
\renewcommand*{\section}[1]{%
\par
\phantomsection{}% reset the anchor for hyperrefs
\addcontentsline{toc}{section}{#1}
\sectionstyle{\cusuline{#1}}%
\par\nobreak\addvspace{1ex}\@afterheading}
\makeatother

\begin{document}

\maketitle

\section{Education}
\section{Key Skills}
\section{Scholarships}

\end{document}

在此处输入图片描述

相关内容