[Tex/LaTex] How to make multiple lines in ToC horizontally align

horizontal alignmenttable of contentstocloft

For single line, the content aligned nicely, but that's not the case when the it contains multiple lines. The alignment is odd:

enter image description here

So how can I make them align nicely?

Mimimum Example

\documentclass[10pt,letterpaper]{article}
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry} 
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

\setcounter{tocdepth}{3}
\setcounter{secnumdepth}{3}
\usepackage[bookmarksopen,bookmarksdepth=3]{hyperref}
\usepackage{titlesec}
\usepackage{xcolor}

%define new colors
\definecolor{dark-red}{rgb}{0.4,0.15,0.15}
\definecolor{dark-blue}{rgb}{0.15,0.15,0.4}
\definecolor{medium-blue}{rgb}{0,0,0.5}

%set up color for table of contents
\hypersetup{
    colorlinks, linkcolor={medium-blue},
    citecolor={dark-blue}, urlcolor={medium-blue}
}

\usepackage{tocloft}

%preven linebreak between subsection header and its content
\usepackage{titlesec}
\titleformat{\subsection}[runin]{\normalfont\bfseries}{\thesubsection.}{3pt}{}
\titleformat{\section}[runin]{\normalfont\bfseries}{\thesection.}{3pt}{}

\begin{document}

\setlength{\cftsecnumwidth}{8em} % Set numwidth of section
\setlength{\cftsubsecnumwidth}{12em} % Make subsection numwidth the same as section
\setlength{\cftsubsecindent}{\cftsecindent} % Make subsection indent the same as section

\tableofcontents 
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex}

    \newpage
    \phantomsection
    \section*{Problem 4.}
    \addcontentsline{toc}{section}
    {
        \numberline{Problem 4.}Let $T$ be a tree with $n > 1$ nodes. What is the maximum number of degrees one vertex $T$ can have?
        What his the minimum number of degree one vertex $T$ can have? Justify both answers.
    }
    Something
\end{document}

Best Answer

In this specific example, you need to remove the spurious space contained in your \addcontentsline definition:

\addcontentsline{toc}{section}
{% <------ Required
    \numberline{Problem 4.}Let $T$ be a tree with $n > 1$ nodes. What is the maximum number of degrees one vertex $T$ can have?
    What his the minimum number of degree one vertex $T$ can have? Justify both answers.
}

ToC

Also note that hyperref complains about: "Token not allowed in a PDF string (PDFDocEncoding): removing 'math shift' on input line XXX". This is because of using math content $ $ within the ToC entry, which eventually end up in the PDF bookmarks. To get rid of this, hyperref provides the conditional \texorpdfstring{<tex-string>}{<pdf-string>}. You would use it in the following way:

\addcontentsline{toc}{section}
{% <------ Required
    \numberline{Problem 4.}Let \texorpdfstring{$T$}{T} be a tree with 
    \texorpdfstring{$n>1$}{n>1} nodes. What is the maximum number of degrees 
    one vertex \texorpdfstring{$T$}{T} can have? What his the minimum number 
    of degree one vertex \texorpdfstring{$T$}{T} can have? Justify both answers.
}
Related Question