2

So I'm trying to make the format of the subsection be normal font size, underlined, and have title case. Below is an example, stolen from here:

\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}

Example Output

As you can see, the section Sampling about something magic makes has the underline, but not the title case. The next section with a long title has the title case once you remove the underline. How do I get both at the same time?

Also, more random note, if the \titlecap isn't at the end of the format arguments, it will not compile. Not sure if that helps out or not.

  • only the last command can have argument. So you need to define your own command as \underline{\titlecap{#1}} – touhami Mar 31 '18 at 21:57
  • \subsectionfont{\normalsize\normalfont\underline{\titlecap{#1}}} didn't work either. It looks like it's complaining about the fact that I'm using a #1without it actually having a command. Is there a way to do what you're talking about in the context of \subsectionfont? – u2berggeist Mar 31 '18 at 22:24
  • No, you have to define new command \UlCap in some way like \underline{\titlecap{#1}} and then use it in \subsectionfont{\normalsize\normalfont\UlCap}. – touhami Mar 31 '18 at 22:28
  • \newcommand{\ULcap}[1]{\underline{\titlecap{#1}}} and \subsectionfont{\normalsize\normalfont\ULcap}doesn't work either. – u2berggeist Mar 31 '18 at 23:31
2

Not with sectsty nor titlecap, but with xparse and 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}

enter image description here

But, please, don't inflict underlining to your readers.

  • Thanks for the code. That's a lot of code for just making an underline... As for the infliction of underlining, that is done by TMS proceedings format. I have no say in it unfortunately. – u2berggeist Apr 1 '18 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.