2

I'm trying to automate the capitalization of titles in sections, subsections, etc. Basically, I'd like to format every title with \capitalize from the stringstrings package. One approach I've tried is to use the \titleformat command from the \titlesec package, but I'm getting errors. For example:

\titleformat{\section}{\normalfont}{\thesection}{1em}{\capitalizetitle}

What am I missing? Maybe I don't know how to properly use the above command.

Any help or suggestions are appreciated!

EDIT

After the suggestions given below, I created an MWE that raises errors when having bibliography citation.

File test.tex

\documentclass{article}

%%% Language support
\usepackage[english]{babel} % Get everything translated properly
\usepackage[latin1]{inputenc} % Input text correctly
\usepackage[T1]{fontenc} % Get hyphenation right
\usepackage{lmodern} % The Latin Modern fonts
%%%

%%% AMS support
\usepackage{amsmath}
\usepackage{amsfonts}   % fonts
\usepackage{amssymb}    % extra symbols
%%%

\usepackage{natbib}
\bibpunct{(}{)}{;}{a}{,}{,}

\usepackage{titlesec,titlecaps}

\Addlcwords{is with}
\titleformat{\section}[block]{}{\normalfont\Large\bfseries\thesection.\;}{0pt}{\formatsectiontitle}
\newcommand{\formatsectiontitle}[1]{\normalfont\Large\bfseries\titlecap{#1}}

\titleformat{\subsection}[block]{}{\normalfont\large\bfseries\thesubsection.\;}{0pt}{\formatsubsectiontitle}
\newcommand{\formatsubsectiontitle}[1]{\normalfont\large\bfseries\titlecap{#1}}


\begin{document}

\section{this is a title 1}

\subsection{this is a Subsection Title 1}

\section{Another title 2}

Please see \cite{author2016}.

\bibliographystyle{plain}
\bibliography{test_bib}

\end{document}

File test_bib.bib:

@Article{author2016,
author = {Last Name, F. N.},
title = {{Title of the Paper}},
journal = {Journal X.},
volume = {130},
number = {20},
pages = {6342-6344},
year = {2016}
}

Then calling:

pdflatex -> works

bibtex -> works

pdflatex -> error

The error is given below:

! Argument of \ttl@gmk has an extra }.

\par
l.1 \begin{thebibliography}{5}
I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.
Runaway argument?

Any ideas of how to fix this?

EDIT 2

Following suggestions, the new file test.tex below compiles with no errors.

\documentclass{article}

%%% Language support
\usepackage[english]{babel} % Get everything translated properly
\usepackage[latin1]{inputenc} % Input text correctly
\usepackage[T1]{fontenc} % Get hyphenation right
\usepackage{lmodern} % The Latin Modern fonts
%%%

%%% AMS support
\usepackage{amsmath}
\usepackage{amsfonts}   % fonts
\usepackage{amssymb}    % extra symbols
%%%

\usepackage{natbib}
\bibpunct{(}{)}{;}{a}{,}{,}

\usepackage{titlesec,titlecaps}

\let\titlecap\relax \def\Addlcwords#1{}

\Addlcwords{is with}
\titleformat{\section}[block]{}{\normalfont\Large\bfseries\thesection.\;}{0pt}{\formatsectiontitle}
\newcommand{\formatsectiontitle}[1]{\normalfont\Large\bfseries\titlecap{#1}}

\titleformat{\subsection}[block]{}{\normalfont\large\bfseries\thesubsection.\;}{0pt}{\formatsubsectiontitle}
\newcommand{\formatsubsectiontitle}[1]{\normalfont\large\bfseries\titlecap{#1}}


\begin{document}

\section{this is a title 1}

\subsection{this is a Subsection Title 1}

\section{Another title 2}

Please see \cite{author2016}.

\bibliographystyle{apalike}
\bibliography{test_bib}

\end{document}

pdflatex -> bibtex -> pdflatex -> pdflatex produces the result below. The problem now is that the titles are not automatically capitalized.

Output

  • 1
    First, I recommend the use of the titlecaps package, rather than stringstrings, for the purpose of achieving Title Case (I wrote both packages). However, in both cases, the macros are not mode commands, but need to take an argument, so I am not sure they are compatible with the approach you are proposing. – Steven B. Segletes Apr 16 '16 at 4:24
2

EDITED (See SUPPLEMENT for compatibility with natbib)

I stand corrected from my comment. It can be done. I followed the example of Chapter title alignment with titleformat but made it to title caps, instead of small caps.

I even employed the word exclusion available from \Addlcwords{}.

In my original comment to the OP, I recommended the use of the titlecaps package, which I wrote precisely because the \capitalizetitle macro of the stringstrings package (which I also wrote) was embarrassingly slow.

\documentclass[12pt]{book}

\usepackage{titlesec}
\usepackage{xcolor,lipsum,titlecaps}

\titleformat{\chapter}[block]
  {}
  {\llap{\color{gray}\chapterNumber\thechapter
   \hspace{10pt}\vline}}
  {10pt}
  {\formatchaptertitle}

\newcommand{\formatchaptertitle}[1]{%
  \parbox[t]{\dimexpr\textwidth-10pt}{\raggedright\LARGE\titlecap{#1}}}

\newcommand{\chapterNumber}{%
  \fontsize{50}{50}\usefont{U}{eur}{b}{n}}

\Addlcwords{is with}
\begin{document}
\chapter{this is my long long long long title with several lowercase words}

\lipsum[1]
\end{document}

enter image description here

SUPPLEMENT

Apparently, the use of \titlecap in section titles via titlesec is incompatible with natbib. The solution here was to \let\titlecap\relax immediately prior to the bibliography call, which essentially turns off titlecap for the bibliography. But since the section name of the bibliography is "References" and does not need \titlecap, there is no harm is so doing.

\documentclass{article}
%%% Language support
\usepackage[english]{babel} % Get everything translated properly
\usepackage[latin1]{inputenc} % Input text correctly
\usepackage[T1]{fontenc} % Get hyphenation right
\usepackage{lmodern} % The Latin Modern fonts
%%%
%%% AMS support
\usepackage{amsmath}
\usepackage{amsfonts}   % fonts
\usepackage{amssymb}    % extra symbols
%%%
\usepackage{natbib}
\bibpunct{(}{)}{;}{a}{,}{,}

\usepackage{titlesec,titlecaps}

\Addlcwords{is with}
\titleformat{\section}[block]{}{\normalfont\Large\bfseries\thesection.\;}{0pt}{\formatsectiontitle}
\newcommand{\formatsectiontitle}[1]{\normalfont\Large\bfseries\titlecap{#1}}

\titleformat{\subsection}[block]{}{\normalfont\large\bfseries\thesubsection.\;}{0pt}{\formatsubsectiontitle}
\newcommand{\formatsubsectiontitle}[1]{\normalfont\large\bfseries\titlecap{#1}}

\begin{document}

\section{this is a title 1}

\subsection{this is a Subsection Title 1}

\section{Another title 2}

Please see \cite{author2016}.

\let\titlecap\relax
\bibliographystyle{apalike}
\bibliography{test_bib}
\end{document}

enter image description here

  • Thank you so much for the suggestions and example, Steven! It's difficult to get a better answer than from the creator of the package! :) – Bruno Apr 17 '16 at 5:06
  • I'm afraid I'm running into errors when I use your solution and have a bibliography section. Please see the edited OP. Thanks! – Bruno Apr 17 '16 at 5:34
  • As you have witnessed, I can't claim infallibility. It would help those trying to answer if you could post a complete (small) example, starting with \documentclass, which demonstrates the issue. As a side note, sometimes the order in which packages are loaded can influence results. – Steven B. Segletes Apr 17 '16 at 10:41
  • I added an MWE. Hopefully it's helpful for debugging. – Bruno Apr 21 '16 at 4:06
  • @Bruno First thing I did was to turn off titlecaps by invoking \let\titlecap\relax \def\Addlcwords#1{} in the preamble to turn off titlecap features. One can even remove its mention in \usepackage. If titlecap were the problem, then this should work. When I try to pdflatex it the 2nd time, I get various errors: I am not a bib expert. Did you invoke bibtex for the 2nd round, or something like natbib? – Steven B. Segletes Apr 21 '16 at 10:08

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.