sectsty 下划线和 titlecap 无法协同工作

sectsty 下划线和 titlecap 无法协同工作

因此,我尝试将子部分的格式设为正常字体大小、下划线并采用标题大小写。下面是一个例子,摘自这里

\documentclass{article}
\usepackage[normalem]{ulem}
\usepackage{sectsty} % Used for section formatting
\usepackage{titlecaps} % used for title case

\sectionfont{\centering\bfseries\normalsize\titlecap}

\begin{document}
\section*{Materials and methods}

\subsectionfont{\normalfont\normalsize\underline\titlecap}
\subsection*{Sampling about something magic}

\subsectionfont{\normalfont\normalsize\titlecap}
\subsection*{A very long sectional heading title that will probably have to be split over two lines}
Lorem ipsum
\end{document}

示例输出

如您所见,该部分Sampling about something magic有下划线,但没有标题大小写。一旦删除下划线,下一个带有长标题的部分就会有标题大小写。我如何同时获得两者?

另外,还有一点需要注意,如果\titlecap格式参数末尾没有,则无法编译。不确定这是否有帮助。

答案1

不是使用sectstynor titlecap,而是使用xparseand titlesec

\documentclass{article}
\usepackage[normalem]{ulem}
\usepackage{titlesec} % Used for section formatting
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\titlecap}{m}
 {
  \berggeist_titlecap:Nn \tl_use:N { #1 }
 }

\cs_new_protected:Nn \berggeist_uline:n { \uline { #1 } }
\cs_generate_variant:Nn \berggeist_uline:n { V }

\NewDocumentCommand{\ultitlecap}{m}
 {
  \berggeist_titlecap:Nn \berggeist_uline:V { #1 }
 }

\NewDocumentCommand{\addtitlecapexceptions}{m}
 {
  \clist_gput_right:Nn \g_berggeist_titlecap_exceptions_clist { #1 }
 }

\seq_new:N \l__berggeist_titlecap_in_seq
\seq_new:N \l__berggeist_titlecap_out_seq
\tl_new:N \l__berggeist_titlecap_out_tl
\clist_new:N \g_berggeist_titlecap_exceptions_clist

\cs_new_protected:Nn \berggeist_titlecap:Nn
 {
  \seq_set_split:Nnn \l__berggeist_titlecap_in_seq { ~ } { #2 }
  \seq_clear:N \l__berggeist_titlecap_out_seq
  \seq_map_inline:Nn \l__berggeist_titlecap_in_seq
   {
    \clist_if_in:NnTF \g_berggeist_titlecap_exceptions_clist { ##1 }
     {
      \seq_put_right:Nn \l__berggeist_titlecap_out_seq { ##1 }
     }
     {
      \seq_put_right:Nf \l__berggeist_titlecap_out_seq { \tl_mixed_case:n { ##1 } }
     }
   }
  \tl_set:Nx \l__berggeist_titlecap_out_tl
   {
    \seq_use:Nn \l__berggeist_titlecap_out_seq { ~ }
   }
  #1 \l__berggeist_titlecap_out_tl
 }
\cs_generate_variant:Nn \seq_put_right:Nn { Nf }

\ExplSyntaxOff

\addtitlecapexceptions{a,an,and,or,to}

\titleformat{\section}[block]
  {\filcenter\bfseries\normalsize}
  {\thesection}
  {1em}
  {\titlecap}

\begin{document}

\section*{Materials and methods}

\titleformat{\subsection}[block]
  {\normalfont\normalsize}
  {\thesubsection}
  {1em}
  {\titlecap}

\subsection*{Sampling about something magic}

\titleformat{\subsection}[block]
  {\normalfont\normalsize}
  {\thesubsection}
  {1em}
  {\ultitlecap}

\subsection*{A very long sectional heading title that
  will probably have to be split over two lines}
Lorem ipsum

\end{document}

在此处输入图片描述

但是,请不要给你的读者带来下划线。

相关内容