5

I want to capitalize the first letter of the first word in the argument of a "\section" \command.

Consider \newcommand{\hello}{hello world}

I want to use

\section{\hello} 

so that the compiled section header is

0.1 Hello world  

I tried

\titleformat{\section}
   {\normalfont\Large\bfseries\expandafter\MakeUppercase }{\thesection}{1em}{}

But that returns HELLO WORLD.

How can I only capitalize the first word so that I get

0.1 Hello world  

Is there a way to carry over the title to the table of contents as well?

\let\TEMPtableofcontents\tableofcontents
\renewcommand{\tableofcontents}{
  \clearemptydoublepage
  \providecommand\phantomsection{} \phantomsection
  \addcontentsline{toc}{chapter}{Table of Contents}
  \TEMPtableofcontents
}
Mico
  • 506,678
A.Dumas
  • 211
  • Would a LuaLaTeX-based solution be acceptable to you? – Mico Nov 10 '22 at 15:55
  • I am quite unsure how lua works. I generally use vim-latex to compile. If it can be integrated without package installation. I will give it a try. – A.Dumas Nov 10 '22 at 15:59
  • No knowledge of Lua is assumed or required in order to compile a document under LuaLaTeX instead of pdfLaTeX. (Of course, such knowledge doesn't hurt either.) The main requirement for successful compilation is that one not load the inputenc and fontenc packages and, instead, load the fontenc package. For more information on this topic, see Frequently loaded packages: Differences between pdfLaTeX and LuaLaTeX. – Mico Nov 11 '22 at 07:17

3 Answers3

6

A simpler version of Skillmon's answer. This should work with every class that has the standard syntax for \section.

\documentclass{article}

\ExplSyntaxOn \cs_new_eq:NN \titlecasefirst \text_titlecase_first:n \ExplSyntaxOff

\NewCommandCopy{\latexsection}{\section} \RenewDocumentCommand{\section}{sO{#3}m}{% \IfBooleanTF{#1}{% \latexsection*{\titlecasefirst{#3}} }{% \latexsection[\titlecasefirst{#2}]{\titlecasefirst{#3}}% }% }

\newcommand{\hello}{hello world}

\begin{document} \tableofcontents \section{\hello} \section[again \hello]{Again \hello{} but much longer} \end{document}

enter image description here

egreg
  • 1,121,712
5

The following works with a (more or less) up to date LaTeX installation.

\documentclass{article}

\ExplSyntaxOn \cs_new_eq:NN \titlecasefirst \text_titlecase_first:n \ExplSyntaxOff

\usepackage{titlesec}

\titleformat{\section} {\normalfont\Large\bfseries}{\thesection}{1em}{\titlecasefirst}

\newcommand{\hello}{hello world}

\begin{document} \section{\hello} \end{document}

But the ToC will not contained the altered heading. To also get the ToC affected the following assumes more or less standard-class behaviour.

It patches the macro formatting section-entries in the Toc (\l@section) to alter its argument to the result as if \section{\protect\titlecasefirst{<title>}} was always used instead of \section{<title>}.

\documentclass{article}

\ExplSyntaxOn \cs_new_eq:NN \titlecasefirst \text_titlecase_first:n \ExplSyntaxOff

\usepackage{titlesec}

\titleformat{\section} {\normalfont\Large\bfseries}{\thesection}{1em}{\titlecasefirst}

\makeatletter \AtBeginDocument {% \NewCommandCopy\l@section@orig\l@section \renewcommand\l@section[1] {% \expandafter\l@section@orig\expanded {{\iffalse{{\fi\l@section@titlecasefirst#1}}}}% }% } \newcommand\l@section@titlecasefirst[2] {% \unexpanded{#1{#2}}% \unexpanded\expandafter {\expandafter\titlecasefirst\expandafter{\iffalse}}\fi } \makeatother

\newcommand{\hello}{hello world}

\begin{document} \tableofcontents \section{\hello} \end{document}

enter image description here

Skillmon
  • 60,462
  • I was about to ask is there way to carry over the changes to the table of content or shall I open a new thread – A.Dumas Nov 10 '22 at 16:14
  • @A.Dumas That would require you to alter the ToC, there are a few possibilities that could do this. Do you use any packages that alter the ToC or do you use just a definition from one of the standard classes? – Skillmon Nov 10 '22 at 16:19
  • I use just a minor addition to add the table of contents and added it to the post – A.Dumas Nov 10 '22 at 16:24
  • @A.Dumas ok, and which class do you use? Ideally your question would show a complete minimal example of your document (similar to the code in my answer, but adapted to your actual documentclass, and including any packages you load to alter the aspects you're asking about, so titlesec and if you'd load it something like tocloft or any other ToC-altering packages you're using). – Skillmon Nov 10 '22 at 16:35
  • That is the thing I use a custom class. I have not found any of your mentioned commands therefore I believe it is standard. – A.Dumas Nov 10 '22 at 16:46
  • @A.Dumas see my edit. If that doesn't work, ask a new question with a complete minimal working example. – Skillmon Nov 10 '22 at 17:10
2

Here's a LuaLaTeX-based solution. No knowledege of Lua (the programming language) is assumed or needed. It works for the table of contents as well as for the main body of the document. It works not only for the arguments of \section direcives, but also for those of \subsection and \subsubsection commands.

If you're new to LuaLaTeX, the only notable changes you'll need to apply to your preamb is (a) to make sure that the fontenc and \inputenc packages are not loaded and (b) the fontspec package is loaded.

enter image description here

% !TEX TS-program = lualatex
\documentclass{report}      % or some other suitable document class
\setcounter{secnumdepth}{3} % just for this sample document 
\setcounter{tocdepth}{3}

\usepackage{luacode} % Define two Lua functions: \begin{luacode}

function makefirstcap ( s )
   return ( string.upper ( s:sub (1,1) ) .. s:sub ( 2 ) )
end
function first2cap  ( s )
   s = tostring ( s )
   s = s:gsub ( "(\\.-section)%s*{%s*(.-)}" , 
                function ( x , y )
                   return ( x .."{".. makefirstcap ( y ) .. "}" ) 
                end )
   return s 
end
\end{luacode}

% Assign 'first2cap' function to LuaTeX's "process_input_buffer" callback
\AtBeginDocument{\directlua{luatexbase.add_to_callback (
   "process_input_buffer" , first2cap , "first2cap" )}}

\usepackage{fontspec} % don't load either fontenc or inputenc

\begin{document}

\tableofcontents
\bigskip\hrule\bigskip

\setcounter{chapter}{2} % just for this example

\section{hello world} 
\subsection {goodbye} \subsubsection{ the end} 
\subsubsection { what?! }
\end{document}
Mico
  • 506,678