# Horizontal alignment of margin notes in nested enumitem list

I would like to be able to 'tag' certain list items in an enumitem list with a mark in the left margin. I would like the marks in the left margin to be horizontally aligned with respect to each other, and vertically aligned with respect to the tagged list item. I've tried Werner's answer here but it doesn't seem to work with enumitem.

In the image above (the frames are just for visualization) I would like the tags for the sub-items to be horizontally aligned with the tags for the main list items and the alignment should not change depending on the width of the the sub-item labels (i.e. (w) is wider than (c) but the tags should remain aligned).

I don't imagine that my code or approach are very good, so a completely different way of doing this would be fine. An explanation would be nice as well as I have spent a fair amount of time trying to figure this out myself but it's beyond me.

\documentclass{article}
\usepackage{enumitem}
\usepackage{xparse}
\usepackage{showframe}

\newlist{mylist}{enumerate}{1}

\setlist[mylist]{
%align=left,
align=parleft,
label = {\protect\mytag\arabic*.},
}

\newlist{mysublist}{enumerate}{1}
\setlist[mysublist]{
%align=left,
align=parleft,
label={\protect\mytag{(\alph*)}},
}

\NewDocumentCommand{\myitem}{ o }{
\IfNoValueTF{#1}
{\let\mytag\relax\item}
{\tagit{#1}\item}
}

\makeatletter
\NewDocumentCommand{\tagit}{ m }{
\def\mytag{\leavevmode\llap{\fbox{\makebox[\marginparwidth][c]{#1}}}\hspace{\@totalleftmargin}}
}
\makeatother

\begin{document}

\begin{mylist}
\myitem First Item.
\myitem[x] Second Item.
\begin{mysublist}
\myitem[x] First Sub-Item.
\myitem Second Sub-Item.
\end{mysublist}
\myitem[x] Third Item.
\myitem Fourth Item.
\end{mylist}

\end{document}


I am not sure if this is what you want (some appropriate space is added using a conditional test on \enit@depth):

\documentclass{article}
\usepackage{enumitem}
\usepackage{xparse}
\usepackage{showframe}

\newlist{mylist}{enumerate}{2}

\setlist[mylist,1]{
%align=left,
align=parleft,
label = {\protect\mytag\arabic*.},
}

%\newlist{mysublist}{enumerate}{1}
\setlist[mylist,2]{
%align=left,
align=parleft,
label={\protect\mytag{(\alph*)}},
}

\NewDocumentCommand{\myitem}{ o }{
\IfNoValueTF{#1}
{\let\mytag\relax\item}
{\tagit{#1}\item}
}
\newlength\mylen
\makeatletter
\NewDocumentCommand{\tagit}{ m }{
\ifnum\enit@depth=1\relax
\setlength\mylen{0pt}%
\else
\ifnum\enit@depth=2\relax
\setlength\mylen{\leftmargini}%
\fi\fi%
\def\mytag{\leavevmode\llap{\fbox{\makebox[\marginparwidth][c]{#1}}\hspace{\mylen}}}}
\makeatother

\begin{document}

\begin{mylist}
\myitem First Item.
\myitem[x] Second Item.
\begin{mylist}
\myitem[x] First Sub-Item.
\myitem Second Sub-Item.
\end{mylist}
\myitem[x] Third Item.
\myitem Fourth Item.
\end{mylist}

\end{document}