[Tex/LaTex] Problems in TOC with appendix as section at the end of a chapter

appendicestable of contents

I have an appendix (section) at the end of Chapter 1 that looks like

Appendix 1.A Baz

However, as you can see in the image below, the TOC doesn't display the appendix counter properly (it overlaps)
Appendix

What I want to achieve in the TOC is

1 Foo
  1.1 Bar.......
  Appendix 1.A Baz...... 

Here is a MWE:

\documentclass{book}

\usepackage{etoolbox}

\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \section{#1}\renewcommand*{\thesection}{\thechapter.\Alph{section}}%
}
\pretocmd{\chapter}{\renewcommand*{\thesection}{\thechapter.\arabic{section}}}{}{}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\chapter{Foo}
\section{Bar}

\secappendix{Baz}
\end{document}

EDIT: The above MWE has further problems when using the hyperref package. The hyperlink from the TOC to the appendix heading (wrongly) takes you the first section and the hyperlink to the equation in the appendix takes you to the equation in the first section. I tried the solution suggested in this answer but it doesn't seem to work (in fact I think it doesn't work in the corresponding MWE there). Here is a new MWE including the hyperref package.

\documentclass{book}

\usepackage{etoolbox}
\usepackage{lipsum}

\usepackage{amsmath}
\numberwithin{equation}{section}

\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \section{#1}\renewcommand*{\thesection}{\thechapter.\Alph{section}}%
}
\preto{\chapter}{\renewcommand*{\thesection}{\thechapter.\arabic{section}}}

\usepackage[colorlinks=true]{hyperref}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\chapter{Foo}
\section{Bar}
\begin{equation}
\label{eq:1}
  1 + 1 = 2
\end{equation}
\lipsum
\secappendix{Baz}
\begin{equation}
\label{eq:2}
  2 + 2 = 4
\end{equation}

Hyperlink to \ref{eq:2}
\end{document}

EDIT 2: Changing the command to

\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \renewcommand*{\theHsection}{\thechapter.\Alph{section}}%
  \section{#1}\renewcommand*{\thesection}{\thechapter.\Alph{section}}%
}

seems to fix the hyperref problem but the TOC issue remains.

Edit 3: Since I've added a bounty I think I should explain the motivation behind this question. I don't want the appendix of a chapter to be at the same level as the chapter since the appendix is (just) a part of the chapter. Therefore I want to treat appendices as sections. I also want to signal that the appendices are in fact appendices. What I want to achieve is something like this

1 Chapter title
  1.1 Section title
      1.1.1 Subsection title
  Appendix 1.A Appendix title
      1.A.1 Appendix subsection title

Best Answer

This solution does not use any package (except the OP already has), but adding a \addtocontents{toc}{...} to the author-defined command \secappendix.

The following command taking 3 parameters is used to set l@section. The default value for numwidth is 2.3em instead of 7em here. One could adjust to suit one's need.

\@dottedtocline{<level>}{<indentation>}{<numwidth>}}

If dotted lines in TOC is to remove, use the following in the preamble.

\makeatletter
\renewcommand\@dotsep{450}
\makeatother

enter image description here

enter image description here

Code

\documentclass{book}

\usepackage{etoolbox}
\usepackage{lipsum}

\usepackage{amsmath}
\numberwithin{equation}{section}

\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \addtocontents{toc}
  {\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{7em}}}
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \section{#1}
  \renewcommand*{\thesection}{\thechapter.\Alph{section}}%
  \addtocontents{toc}
  {\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{2.3em}}}
}
\preto{\chapter}{\renewcommand*{\thesection}{\thechapter.\arabic{section}}}

\usepackage[colorlinks=true]{hyperref}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\chapter{Foo}

Here is a link to \ref{app:baz}.

\section{Bar}
\begin{equation}
\label{eq:1}
  1 + 1 = 2
\end{equation}
\subsection{Subsection title}
\lipsum[1]

\secappendix{Baz}
\label{app:baz}
\begin{equation}
\label{eq:2}
  2 + 2 = 4
\end{equation}
\subsection{Appendix subsection title}
\lipsum[1]
Hyperlink to \ref{eq:2}

\chapter{Chapter 2}
\section{Test}
\subsection{Test subsection title}
\secappendix{TestBaz}
\subsection{TestBaz subsection title}

\end{document}

Edit: The OP wishes to put the author-defined section name into the bookmark as well. To this end, this solution seeks help from bookmark package and please load it after hyperref package to avoid crash. Then add \pdfbookmark[section]{\thesection~#1}{#1}, respectively for <level><bookmark text><internal label>, into the author-defined macro as shown below.

\usepackage[colorlinks=true]{hyperref}
\usepackage{bookmark}                       % <-- here

\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \addtocontents{toc}
  {\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{7em}}}
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \section{#1}
  \pdfbookmark[section]{\thesection~#1}{#1}  %<-- Here
  \renewcommand*{\thesection}{\thechapter.\Alph{section}}%
  \addtocontents{toc}
  {\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{2.3em}}}
}

Final Update: It seems that the OP-defined macro will by itself insert a bookmark whenever \section{#1} is called, resulting in repeated bookmark if the bookmark package is used. On the first notice by the OP, an investigation is activated. To remove the redundant bookmark, this solution modifies the OP-defined macro (marked by <-- here) without using bookmark which causes the redundancy. The objective wanted by OP is achieved and therefore post the full code again to avoid ambiguity.

enter image description here

** Full Code:**

\documentclass{book}

\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{amsmath}
\numberwithin{equation}{section}

\newcommand*{\secappendix}[1]{%
\setcounter{section}{1}                                   %<-- here
\setcounter{subsection}{0}                                %<-- here
\addtocontents{toc}
{\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{7em}}}
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
\section*{\thesection~#1}                                 %<-- here
\phantomsection                                           %<-- here
\addcontentsline{toc}{section}{\thesection~#1}            %<-- here
\renewcommand*{\thesection}{\thechapter.\Alph{section}}%
\addtocontents{toc}
{\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{2.3em}}}
}
\preto{\chapter}{\renewcommand*{\thesection}{\thechapter.\arabic{section}}}

\usepackage[colorlinks=true]{hyperref}
%\usepackage{bookmark}

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\chapter{Foo}

Here is a link to \ref{app:baz}.

\section{Bar}
\begin{equation}
\label{eq:1}
  1 + 1 = 2
\end{equation}
\subsection{Subsection title}
\lipsum[1]

\secappendix{Baz}
\label{app:baz}
\begin{equation}
\label{eq:2}
  2 + 2 = 4
\end{equation}
\subsection{Appendix subsection title}
\subsection{Appendix second subsection title}
\lipsum[1]
Hyperlink to \ref{eq:2}

\chapter{Indentation}
\lipsum[1]
\section{Test}
\lipsum[1]
\subsection{Test subsection title}
\lipsum[1]
\secappendix{TestBaz}
\lipsum[1]
\subsection{TestBaz subsection title}
\subsection{TestBaz second subsection title}

\chapter{Bookmark}
\lipsum[1]
\section{Test2}
\lipsum[1]
\subsection{Test2 subsection title}
\lipsum[1]
\secappendix{Test2}
\lipsum[1]
\subsection{Test2 subsection title}
\subsection{Test2 second subsection title}
\end{document}
Related Question