Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I restyled the \section and \subsection a little bit, to better suite my needs when writing assignments. Using titlesec, I moved the (sub)section number to the right of the (sub)section title. In my preamble:

\usepackage[compact,explicit]{titlesec}

\def\thesection{\Roman{section}}
\titleformat{\section}[hang]{\Large\bfseries}{}{0pt}{#1 \thesection}
\def\thesubsection{\roman{subsection}:}
\titleformat{\subsection}[runin]{\large\itshape}{}{0pt}{#1 \thesubsection:}

In my documents, I usually use \section{Part} and \subsection{Question}, depending on the assignment. The advantage of course is, that I don't have to worry about Question numbering.

However, the problem is that in the table of contents the (sub)section number is of course still displayed on the left, while I want both on the right as well.

I looked at the tocloft package, but I can't figure out this particular case.

share|improve this question
1  
A tip: You can use backticks ` to mark your inline code as I did in my edit. –  egreg Nov 20 '12 at 23:28
    
I will keep it in mind for next time, thanks. –  altabq Nov 21 '12 at 11:56
add comment

1 Answer

up vote 6 down vote accepted

Here's a possible solution; the idea is to set tocdepth to 0 inside the redefinition for the section headings so they won't be automatically included in the ToC; then the titles are manually included using \addcontentsline with the desired formatting (title, number, page number); finally, the etoolbox package is used to restore the tocdepth counter to its default value:

\documentclass{article}
\usepackage[compact,explicit]{titlesec}
\usepackage{etoolbox}
\pretocmd{\subsection}{\addtocontents{toc}{\setcounter{tocdepth}{2}}}{}{}

\def\thesection{\Roman{section}}
\def\thesubsection{\roman{subsection}}

\titleformat{\section}[hang]
  {\Large\bfseries}{}{0pt}{#1 \thesection%
    \addcontentsline{toc}{section}{#1~\numberline{\thesection}}
}[\addtocontents{toc}{\setcounter{tocdepth}{0}}]
\titleformat{name=\section,numberless}[hang]
  {\Large\bfseries}{}{0pt}{#1}
\titleformat{\subsection}[runin]
  {\large\itshape}{}{0pt}{#1 \thesubsection:}
\titleformat{name=\subsection,numberless}[runin]
  {\large\itshape}{}{0pt}{#1:}

\begin{document}

\tableofcontents
\section{Test Section One}
\subsection{Test}
\section{Test Section Two}

\end{document}

enter image description here

Not relevant to the problem mentioned, but I provided also definitions to be applied to the unnumbered sections and subsections (produced with \section* and \subsection*) to prevent numbering them.

Another option, is to use a new auxiliary file (with extension .moc, for example) and redefine \tableofcontents so that \@starttoc uses moc instead of toc; this simple trick simplifies the solution (no patching required) and it is more suitable in case the new formatting has to be applied to more sectional units (as was requested in a comment to the original answer); here's a complete example illustrating this approach:

\documentclass{article}
\usepackage[compact,explicit]{titlesec}


\def\thesection{\Roman{section}}
\def\thesubsection{\roman{subsection}}

\titleformat{\section}[hang]
  {\Large\bfseries}{}{0pt}{#1 \thesection%
    \addcontentsline{moc}{section}{#1~\numberline{\thesection}}
}
\titleformat{name=\section,numberless}[hang]
  {\Large\bfseries}{}{0pt}{#1}
\titleformat{\subsection}[runin]
  {\large\itshape}{}{0pt}{#1 \thesubsection:%
    \addcontentsline{moc}{subsection}{#1~\numberline{\thesubsection}}}
\titleformat{name=\subsection,numberless}[runin]
  {\large\itshape}{}{0pt}{#1:}

\makeatletter
\renewcommand\tableofcontents{%
    \section*{\contentsname
        \@mkboth{%
           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \@starttoc{moc}%
    }
\makeatother
\begin{document}

\tableofcontents
\section{Test Section One}
%\addtocontents{tocdepth}{0}
\subsection{Test Subsection A}
\subsection{Another Test Subsection A}
\section{Test Section Two}
\subsection{Test Subsection B}
\subsection{Another Test Subsection B}

\end{document}

enter image description here

share|improve this answer
    
Thanks a lot for this. I tried to replicated the answer to move subsection titles in the tableofcontents to the right as well, but couldn't get it to work. could you extent your example to fit that in as well please? –  altabq Nov 21 '12 at 10:28
1  
@altabq of course. Please see my updated answer; the last code contains a more general approach; the nice thing is that this more general approach is simpler than the particular case I initially addressed. –  Gonzalo Medina Nov 21 '12 at 15:59
    
thank you very much! I added your code to my preamble and it works flawlessly! –  altabq Nov 22 '12 at 23:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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