Exclude section numbers from links in ToC: non-numbered sections bug

I use etoc to exclude section numbers from links in ToC. However, when I insert a non-numbered appexdix section — etoc adds a period:

I played with the code, and found that the problem is with \etocthenumber in \etocsetstyle{section} definition (without etoc the dot is not there). Is there a fix?

Here an MWE:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{xcolor}

\usepackage{titlesec}
\titleformat{\section}
{\color{teal}\large\sffamily}
{\thetitle.\hspace{0.5cm}}
{0cm}
{}

\titleformat{\subsection}
{\color{violet}\bfseries}
{\hspace{0.75cm}\thetitle.\hspace{0.3cm}}
{0cm}
{}

\titleformat{\subsubsection}
{\color{olive}\itshape}
{\hspace{1.3cm}\thetitle.\hspace{0.3cm}}
{0cm}
{}

% ToC styles:
\usepackage[dotinlabels]{titletoc}
\contentsmargin{2.55em} % space for a page number

\titlecontents{section}
[3.8em] % 1.5em + 2.3em
{\vspace{0.3cm}\sffamily\bfseries\large}
{\contentslabel{2.3em}}
{\hspace*{-2.3em}}
{\normalsize\normalfont\rmfamily\titlerule*[1pc]{.}\contentspage}
[\vspace{0.1cm}]

\titlecontents{subsection}
[5.2em] % 3.8em + 1.3em
{\vspace{0.1cm}}
{\contentslabel{1.4em}}
{\hspace*{-1.4em}}
{\titlerule*[1pc]{.}\contentspage}
[]

\titlecontents{subsubsection}
[6.5em] % 5.2em + 1.3em
{\vspace{0.1cm}}
{\contentslabel{1.3em}}
{\hspace*{-1.3em}}
{\titlerule*[1pc]{.}\contentspage}
[]

\makeatletter
\let\perhapsprint\@firstofone
{\glossary\@gobble}
{\glossary\@gobble\let\perhapsprint\@gobble}
{}{}
\makeatother
%\newcommand{\perhapsprint}[1]{\ifprintprefix#1\fi}
%\newif\ifprintprefix
\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{%
\perhapsprint{\Roman{section}.}%
\alph{subsection}%
}
\renewcommand\thesubsubsection{%
\perhapsprint{\Roman{section}.\alph{subsection}.}%
\arabic{subsubsection}%
}

% appendix:
\usepackage[toc,page]{appendix}
\renewcommand*\thesubsection{\Alph{subsection}}

\usepackage{hyperref}

% exclude number from the link in ToC:
\usepackage{etoc}
\makeatletter
% store the original table of contents macros (they are not modified by hyperref, only \contentsline is)
\let\latchapter\l@chapter
\let\latsection\l@section
\let\latsubsection\l@subsection
\let\latsubsubsection\l@subsubsection
% then use the etoc package to feed the l@ macros with a filtered out data
\etocsetstyle{chapter}{}{}
{\latchapter{\numberline{\etocthenumber}\etocname}{\etocpage}}{}
\etocsetstyle{section}{}{}
{\latsection{\numberline{\etocthenumber}\etocname}{\etocpage}}{}
\etocsetstyle{subsection}{}{}
{\latsubsection{\numberline{\etocthenumber}\etocname}{\etocpage}}{}
\etocsetstyle{subsubsection}{}{}
{\latsubsubsection{\numberline{\etocthenumber}\etocname}{\etocpage}}{}
\makeatother

\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\alph{subsection}}
\renewcommand\thesubsubsection{\arabic{subsubsection}}
\renewcommand\theparagraph{\Roman{section}.\alph{subsection}.\arabic{subsubsection}.\roman{paragraph}}

\begin{document}

\tableofcontents

\section{H1}
\subsection{H2}
\subsubsection{H3}
\section{H1}
\subsection{H2}
\subsubsection{H3}

\renewcommand{\appendixtocname}{Appendices}
\renewcommand{\appendixpagename}{Appendices}
\begin{appendices}
\subsection{H2}
\subsubsection{H3}
\subsection{H2}
\subsubsection{H3}
\subsection{H2}
\subsubsection{H3}
\end{appendices}

\end{document}


(I'm also excluding section numbering in ToC)

• You are mixing titletoc with etoc and that's not right. Remove all the stuff related to etoc and the problem will disappear. – karlkoeller Feb 18 '15 at 10:24
• @karlkoeller: but I need etoc to exclude numbers from links to the sections. Is it possible to replace titletoc declarations with etoc ones? – Adobe Feb 18 '15 at 10:35
• Note that with your titletoc settings numbers are excluded from links, without the need of etoc – karlkoeller Feb 18 '15 at 11:03
• @karlkoeller: OMG! you right! – Adobe Feb 18 '15 at 11:56

As mentioned in comments, in view of the other packages used by the OP's code, the simplest remedy is probably to drop use of etoc.

In case some users of etoc find this question, I nevertheless provide this answer to explain what could be the way with etoc to handle the problem: just use the \etocifnumbered command.

\documentclass{article}

% appendix:
\usepackage[toc,page]{appendix}
\renewcommand*\thesubsection{\Alph{subsection}}

\usepackage{hyperref}

% exclude number from the link in ToC:
\usepackage{etoc}
\makeatletter
% store the original table of contents macros (they are not modified by hyperref, only \contentsline is)
\let\latchapter\l@chapter
\let\latsection\l@section
\let\latsubsection\l@subsection
\let\latsubsubsection\l@subsubsection

\etocsetstyle{chapter}{}{}
{\etocifnumbered
{\latchapter{\numberline{\etocthenumber}\etocname}{\etocpage}}
{\latchapter{\etocname}{\etocpage}}}{}

\etocsetstyle{section}{}{}
{\etocifnumbered
{\latsection{\numberline{\etocthenumber}\etocname}{\etocpage}}
{\latsection{\etocname}{\etocpage}}}{}

\etocsetstyle{subsection}{}{}
{\etocifnumbered
{\latsubsection{\numberline{\etocthenumber}\etocname}{\etocpage}}
{\latsubsection{\etocname}{\etocpage}}}{}

\etocsetstyle{subsubsection}{}{}
{\etocifnumbered
{\latsubsubsection{\numberline{\etocthenumber}\etocname}{\etocpage}}
{\latsubsubsection{\etocname}{\etocpage}}}{}

\makeatother

\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\alph{subsection}}
\renewcommand\thesubsubsection{\arabic{subsubsection}}
\renewcommand\theparagraph{\Roman{section}.\alph{subsection}.\arabic{subsubsection}.\roman{paragraph}}

\begin{document}

\tableofcontents

\section{H1}
\subsection{H2}
\subsubsection{H3}
\section{H1}
\subsection{H2}
\subsubsection{H3}

\renewcommand{\appendixtocname}{Appendices}
\renewcommand{\appendixpagename}{Appendices}
\begin{appendices}
\subsection{H2}
\subsubsection{H3}
\subsection{H2}
\subsubsection{H3}
\subsection{H2}
\subsubsection{H3}
\end{appendices}

\end{document}