[Tex/LaTex] Hide \thechapter for unnumbered Chapters

formattingsectioningtitlesec

I want to display my Chapter-Numbers on the right below the Title. I came to the following minimal working example:

\documentclass{article}
\usepackage{titlesec}
\usepackage{marginnote}
\usepackage{lipsum} % for dummy text only
\titleformat{\section}[block]{\LARGE\selectfont}{}{0em}{}[\marginnote{\Huge\thesection}]

\begin{document}
    \section{First section with\\linebreak}
        \lipsum[2]
    \section*{Unnumbered}
        \lipsum[12]
    \section{Three}
        \lipsum[5]
\end{document}

However the unnumbered section now gets numbered as 1. How can I suppress \thesection for unnumbered sections?
When I move the \marginnote to the <label> field in \titleformat the Number 'ignores' that the Title contains linebreaks and is positioned relative to the first line.

EDIT:
As an example, this is my code for the chapter. Using your solution – which works without an \if – I get the Error “\begin{document} missing“.

\titleformat{\chapter}[block]{\fontsize{40}{10}}{}{0em}{#1}[
    \begin{textblock}{2}(13.7,3.2)
        \color[rgb]{0.39453,0.48047,0.511719}   % Error Happens Here
        \thechapter
    \end{textblock}
]

As soon as I add a \if around the block, the error disappears.

\titleformat{\chapter}[block]{\fontsize{40}{10}}{}{0em}{#1}[
    \iftrue{
        \begin{textblock}{2}(13.7,3.2)
            \color[rgb]{0.39453,0.48047,0.511719}   % No Error Happens with if
            \thechapter
        \end{textblock}
    }
    \fi
]

Best Answer

I would opt for a solution that is semantically more clear. titlesec provides the option to specify a different style for numberless sections.

Copy your original command, specify numberless, remove the things you don't want to show (like section numbers) and you're done!

\usepackage[explicit]{titlesec}
% Regular sections:
\titleformat{\section}[block]{\LARGE\selectfont}{}{0em}{}[\marginnote{\Huge\thesection}]
% ...etc

% Numberless sections:
\titleformat{name=\section,numberless}[block]{\LARGE\selectfont}{}{0em}{#1}
% ...etc
Related Question