2

I used this and trying to put underline as shown here

This works alone perfectly but whenever I try to add the line

\titleformat*{\section}{\bfseries\sectionfont}{\thesubsection}{1em}{#1}[{\titlerule[0.8pt]}]

instead of

\titleformat*{\section}{\bfseries\sectionfont} it says

Missing \begin{document}. ...ion}{\bfseries\sectionfont}{\thesubsection

\documentclass[12pt,a4paper]{article}

\usepackage[explicit]{titlesec} % control over title section
\usepackage{fontspec}       % change to system font
\usepackage{anyfontsize}    % changing font size any
\usepackage{graphicx}   % import figure
\usepackage{setspace}   % line gap => \setstretch
\usepackage{parskip}    % linegap => \linespread
\usepackage{gensymb}    % degree symbol
\usepackage{ragged2e}   % Text alignment
\usepackage{xcolor} % changing color
\usepackage{sectsty}    % underline section

\definecolor{Nblack}{rgb}{0,0,0}
\newfontfamily\sectionfont[Color=Nblack,Scale=1.34]{Arial}
\newfontfamily\subsectionfont[Color=Nblack,Scale=1.34]{Garton Medium}
\newfontfamily\subsubsectionfont[Color=Nblack,Scale=1.34]{Garton Medium}

\titleformat{\section}{\bfseries\sectionfont}{\thesubsection}{1em}{#1}[{\titlerule[0.8pt]}]
\titleformat{\subsection}{\subsectionfont}
\titleformat{\subsubsection}{\itshape\subsubsectionfont}

\begin{document}
\section{Introduction}
\end{document}

or if I remove [explicit] and add * after \titleformat it gives

Illegal parameter number in definition of \ttlf@section. ...esubsection}{1em}{#1}[{\titlerule[0.8pt]}]

Also, like to have First Word Big caps and others small caps

5
  • Welcome to TeX.SX! Your example is incomplete -- \documentclass is missing, and other issues .... – user31729 Aug 24 '16 at 14:11
  • Why did you edit my changes to improve your format away? :-( – user31729 Aug 24 '16 at 14:14
  • oops, maybe I'm editing at the same time, sorry. and how to write code structure in block? – Mr.EU Aug 24 '16 at 14:16
  • Hi @Runar, it's a very nice edit. How you are doing the "Missing..." part and code part? It's really looking great. – Mr.EU Aug 25 '16 at 4:19
  • That would be a blockquote, one of the buttons above the textfield when posting. – Runar Aug 25 '16 at 4:25
1

The syntax of \titleformat and \titleformat* is very different; the latter just takes two arguments, the former is more complex.

One rarely needs explicit: the last mandatory argument to \titleformat can end with a one-parameter macro, as in the example below.

Beware also that titlesec and sectsty are incompatible with each other. Since you need the former for its advanced features, don't load sectsty.

\documentclass[12pt,a4paper]{article}

\usepackage{fontspec}       % change to system font
\usepackage{titlesec} % control over title section

\ExplSyntaxOn
\NewDocumentCommand{\firstword}{m}
 {
  \seq_set_split:Nnn \l_tmpa_seq {~} {#1}
  \seq_pop_left:NN \l_tmpa_seq \l_tmpa_tl
  \seq_put_left:Nn \l_tmpa_seq {\MakeUppercase{\l_tmpa_tl}}
  \seq_use:Nn \l_tmpa_seq {~}
 }
\ExplSyntaxOff

\newfontfamily\sectionfont{TeX Gyre Heros}
\newfontfamily\subsectionfont{TeX Gyre Heros}

\titleformat{\section}
  {\bfseries\scshape\sectionfont}
  {\thesection}
  {1em}
  {}
  [{\titlerule[0.8pt]}]
\titleformat*{\subsection}{\subsectionfont}
\titleformat*{\subsubsection}{\itshape\subsectionfont}

\begin{document}
\section{Introduction and something else}
\subsection{Subsection}
\subsubsection{Subsubsection}
\end{document}

I used fonts I have on my system (and I recommend you not to use Garton).

The \firstword macro splits the argument at spaces, isolates the first item, encloses it into \MakeUppercase and finally reinstates it for printing all items separated by spaces.

enter image description here

2
  • 1. It's running alone, but with not with my doc. 2. I tried to find what's wrong and got \usepackage{sectsty} as the culprit. 3. Thank you for your efforts. 4. I'm very new to TEX, can you pl suggest any good macro tutorial. (I didn't understand single bit of ExplSyntax) 5. Please elaborate the working of these:{1em}{} Again thank you very much. – Mr.EU Aug 25 '16 at 4:16
  • @Mr.EU, the stuff between ExplSyntaxOn and ExplSyntaxOff is part is using the new and experimental(but stable) syntax from LaTeX3. The same code could ofcourse be written using more accoustomed latex-macros, possibly using xstring. Ill have a look at it later today, when I get to a computer. Regarding {1em}{}, the manual for titlesec explains it really well. – Runar Aug 25 '16 at 4:34

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.