\documentclass[
    % use style = plain,
    title in boldface,
    % title in sffamily,
    emphasize theorems,
    simple name, % not suitable for French, German, etc.
    name in link,
]{einfart}
\usepackage{ProjLib}

\setmonofont[Scale=.9]{Iosevka}

\usepackage[crefthe]{cleveref-forward}
\usepackage{changepage,todonotes,marginnote}

%%================================
%% For typesetting code
%%================================
\usepackage{listings}
\usepackage{xcolor}
\usepackage{setspace}
\definecolor{code-main}{RGB}{70,130,180}
\definecolor{code-expl3}{RGB}{240,50,60}
\definecolor{code-option}{RGB}{40,110,20}
\definecolor{code-keys}{RGB}{100,130,150}
\definecolor{code-comment}{RGB}{20,120,80}
\definecolor{code-background}{gray}{0.99}
\lstset{
    language = [LaTeX]TeX,
    basicstyle = \ttfamily,
    keywordstyle = \color{code-main},
    commentstyle = \color{code-comment},
    showstringspaces = false,
    breaklines = true,
    frame = lines,
    backgroundcolor = \color{code-background},
    basewidth = .5em,
    escapeinside = {(*}{*)},
    alsoletter = {_:},
    % numbers = left,
    % firstnumber = last,
    numberstyle = \scriptsize\ttfamily,
    stepnumber = 1,
    numbersep = 5pt,
}
\newcommand{\meta}[1]{$\langle${\normalfont\itshape#1}$\rangle$}
\lstset{% LaTeX2 commands
    classoffset = 0,
    texcsstyle =* \color{code-main},
    moretexcs =
      {
        ExplSyntaxOn,ExplSyntaxOff,
        NewDocumentCommand,NewDocumentEnvironment,
        cref,Cref,
        crefthe,Crefthe,
        cpageref,
        cpagerefthe,
        namecref,nameCref,
        labelcref,labelCref,
        restorelabel,
        SetUsedOnMessageText,
        SetUsedByMessageText,
        SetUsedByAndOnMessageText,
        SetForwardReferenceStyle,
        SetForwardReferenceStyleInsideMath,
        SetForwardReferenceStyleOutsideMath,
        SetForwardReferenceRefForm,
        SetForwardReferencePagerefForm,
        todo,
        marginnote,
      }
}
\lstset{% LaTeX3 commands
    classoffset = 1,
    texcsstyle =* \color{code-expl3},
    moretexcs =
      {
        tl_new:N,
        tl_set:Nn,
        cs_undefine:c,
      }
}
\lstnewenvironment{code}{\setstretch{1.05}\LocallyStopLineNumbers}{\ResumeLineNumbers\vspace{-.3\baselineskip}\vspace{-.5\parskip}}
\lstnewenvironment{code*}{\setstretch{1.05}\lstset{numbers=left}\LocallyStopLineNumbers}{\ResumeLineNumbers\vspace{-.3\baselineskip}\vspace{-.5\parskip}}

\newcommand{\packageoption}[1]{\textcolor{code-option}{\texttt{#1}}}
\newcommand{\commandoption}[1]{\textcolor{code-keys}{\texttt{#1}}}

%%================================
%% tip
%%================================
\usepackage[many]{tcolorbox}
\newenvironment{tip}[1][Tip]
  {%
    \LocallyStopLineNumbers%
    \begin{tcolorbox}[breakable,
        enhanced,
        width = \textwidth,
        colback = paper, colbacktitle = paper,
        colframe = gray!50, boxrule=0.2mm,
        coltitle = black,
        fonttitle = \sffamily,
        attach boxed title to top left = {yshift=-\tcboxedtitleheight/2, xshift=.5cm},
        boxed title style = {boxrule=0pt, colframe=paper},
        before skip = 3mm,
        after skip = 3mm,
        top = 2.5mm,
        bottom = 1.5mm,
        title={\scshape\sffamily #1}]%
  }
  {%
    \end{tcolorbox}%
    \ResumeLineNumbers%
  }


\title{\textsc{demonstration on the usage and configuration of}\\[.3\baselineskip]\fbox{\textsf{cleveref-forward}}}
\author{}
\date{\today[only-year-month]}

\begin{document}

\maketitle

\section{Demonstration with the default configuration}

\subsection{Theorem-like environments}

\begin{lemma}\label{lem:result}
    Some preliminary result (see the equation \labelcref[used by]{eq:1}).
\end{lemma}

\begin{theorem}\label{thm:test}
    Text.
\end{theorem}
\begin{proof}\restorelabel{thm:test} % Notice the use of \restorelabel here
    Some proof that involves the \cref[used by]{lem:result} and the equations \labelcref[used by and on]{eq:Euler}, \labelcref[used by]{eq:1,eq:2} in the next section. Here, \lstinline|\restorelabel| makes sure that this \texttt{proof} environment is being recognized as the theorem itself.
\end{proof}

\subsection{Displayed equations}

By default, the forward referencing infomation is placed immediately after the equation.

\begin{equation}\label{eq:Euler}
    \mathrm{e}^{\mathrm{i}\pi} + 1 = 0.
\end{equation}

However, with this method, for multilined equations with multiple references, only the last reference will be shown:
\begin{align}
    1 &= 0 + 1 \label{eq:1}\\
    2 &= 1 + 1 \label{eq:2}
\end{align}

This can be overcome if you are willing to place the text message in the margin, see the section on customization.

\subsection{Other types}

It can also be used for some other types, such as items in \texttt{enumerate} list, as long as \textsf{cleveref} is properly configured for that type.

\section{Specify a default option}

If you wish to apply the mode \commandoption{used on}, or \commandoption{used by}, or \commandoption{used by and on} to all \lstinline|\cref| (and \lstinline|\labelcref|, etc.), you can use the package option \packageoption{default=}\meta{mode}. Then you don't need to write it everywhere. And if you wish to manually change this \meta{mode} somewhere, you can still specify it as the command option; and if you don't want the message to display for some references, you may also use the option \commandoption{no use} (or \commandoption{not use}) on the corresponding \lstinline|\label| to disable the related messages.


\section{Customizations}

\begingroup

Here is how you change the appearing text:

\begin{code}
\SetUsedOnMessageText{Appears on~#1.}
\SetUsedByMessageText{Appears in~#1.}
\SetUsedByAndOnMessageText{Appears in~#1 on~#2.}
\end{code}

\SetUsedOnMessageText{Appears on~#1.}
\SetUsedByMessageText{Appears in~#1.}
\SetUsedByAndOnMessageText{Appears in~#1 on~#2.}

\bigskip
And here is an example of setting the message style with the package \textsf{todonotes}:

\begin{code}
\SetForwardReferenceStyle
  {%
    \todo[%
      inline,
      size=\scriptsize,
      color=blue!40,
      backgroundcolor=white,
      textcolor=blue!50!cyan,
      bordercolor=blue!50!cyan,
      noprepend]{%
      \textup{#1}%
    }%
  }
\SetForwardReferenceStyleOutsideMath
  {%
    \todo[%
      inline,
      size=\scriptsize,
      caption={}, % <-- needed, otherwise there would be errors
      color=blue!40,
      backgroundcolor=white,
      textcolor=blue!50!cyan,
      bordercolor=blue!50!cyan,
      noprepend]{%
      \textup{#1}%
    }%
  }
\end{code}

\SetForwardReferenceStyle
  {%
    \todo[%
      inline,
      size=\scriptsize,
      color=blue!40,
      backgroundcolor=white,
      textcolor=blue!50!cyan,
      bordercolor=blue!50!cyan,
      noprepend]{%
      \textup{#1}%
    }%
  }
\SetForwardReferenceStyleOutsideMath
  {%
    \todo[%
      inline,
      size=\scriptsize,
      caption={}, % <-- needed, otherwise there would be errors
      color=blue!40,
      backgroundcolor=white,
      textcolor=blue!50!cyan,
      bordercolor=blue!50!cyan,
      noprepend]{%
      \textup{#1}%
    }%
  }

\bigskip
We use the same example:

\begin{lemma}\label{lem:result'}
    Some preliminary result (see the equation \labelcref[used by]{eq:1'}).
\end{lemma}

\begin{theorem}\label{thm:test'}
    Text.
\end{theorem}
\begin{proof}\restorelabel{thm:test'} % Notice the use of \restorelabel here
    Some proof that involves the \cref[used by]{lem:result'} and the equations \labelcref[used by and on]{eq:Euler'}, \labelcref[used by]{eq:1',eq:2'}.
\end{proof}

\begin{equation}\label{eq:Euler'}
    \mathrm{e}^{\mathrm{i}\pi} + 1 = 0.
\end{equation}
\vspace{-\baselineskip}
\begin{align}
    1 &= 0 + 1 \label{eq:1'}\\
    2 &= 1 + 1 \label{eq:2'}
\end{align}

\bigskip
As you might have noticed, \lstinline|\SetForwardReferenceStyleOutsideMath| is for setting the style of those messages that appear outside the equations.

There is also a \lstinline|\SetForwardReferenceStyleInsideMath|, which triggers the message immediately at where you placed the label. This way it is only plausible to put the message inside the margin. For example, with the package \textsf{marginnote} loaded, you may configure:

\begin{code}
\SetForwardReferenceStyle
  {%
    \marginnote{\normalfont#1}%
  }
\SetForwardReferenceStyleInsideMath
  {%
    \marginnote{\normalfont#1}%
  }
\end{code}

\SetForwardReferenceStyle
  {%
    \marginnote{\normalfont#1}%
  }
\SetForwardReferenceStyleInsideMath
  {%
    \marginnote{\normalfont#1}%
  }

\bigskip
The result would be somewhat like the following (assuming that your document has margins set wide enough):

\vspace{-\baselineskip}
\setlength{\marginparwidth}{20em}
\setlength{\marginparsep}{-15em}
\begin{adjustwidth}{}{16em}
    \begin{lemma}\label{lem:result''}
        Some preliminary result (see the equation \labelcref[used by]{eq:1''}).
    \end{lemma}

    \begin{theorem}\label{thm:test''}
        Text.
    \end{theorem}
    \begin{proof}\restorelabel{thm:test''} % Notice the use of \restorelabel here
        Some proof that involves the \cref[used by]{lem:result''} and the equations \labelcref[used by and on]{eq:Euler''}, \labelcref[used by]{eq:1'',eq:2''}.
    \end{proof}

    \begin{equation}\label{eq:Euler''}
        \mathrm{e}^{\mathrm{i}\pi} + 1 = 0.
    \end{equation}
    \begin{align}
        1 &= 0 + 1 \label{eq:1''}\\
        2 &= 1 + 1 \label{eq:2''}
    \end{align}
\end{adjustwidth}

\bigskip
\begin{tip}[Attention]
    Please pay attention to the usage of \lstinline|#1| and \lstinline|#2| in the configurations.
\end{tip}

\clearpage
By the way, here is the default setting for the style:

\begin{code}
\SetForwardReferenceStyle
  {%
    \emph{#1} \\[.3\baselineskip]%
  }
\SetForwardReferenceStyleOutsideMath
  {%
    \begin{flushright}%
      \emph{#1}%
    \end{flushright}%
    \vspace{.15\baselineskip}%
  }
\end{code}

\endgroup

\SetForwardReferenceStyle
  {%
    \marginnote{\normalfont#1}%
  }
\SetForwardReferenceStyleInsideMath
  {%
    \marginnote{\normalfont#1}%
  }

%% You can safely ignore the code between \ExplSyntaxOn and \ExplSyntaxOff
%% They are just for resetting the theorem referencing style for the rest of the document
\ExplSyntaxOn
\input{create-theorem-preset-names-cleveref} % simple-name mode is not suitable for French, German, etc.
\exp_args:No \SetTheorem { \c_projlib_theorem_supported_clist, theorem-with-name } % remove the text style in the references
  {
    name style = {
      , crefname style = { }
      , Crefname style = { }
      , numbering style = { }
    }
  }
\ExplSyntaxOff


\section{Besides writing in English}

When writing in some European languages, it is possible to specify the definite article and/or declension in the optional argument of \lstinline|\cref| to ensure that the grammar is correct, provided that you have enabled the package option \packageoption{crefthe}.

\subsection{Example in French}

\UseLanguage{French}

\vspace{-1.5\baselineskip}
\setlength{\marginparwidth}{24em}
\setlength{\marginparsep}{-18em}
\begin{adjustwidth}{}{20em}
    \begin{lemma}\label{lem:result_fr}
        Quelques résultats préliminaires (voir l'équation \labelcref[used by]{eq:1_fr}).
    \end{lemma}

    \begin{theorem}\label{thm:test_fr}
        Texte.
    \end{theorem}
    \begin{proof}\restorelabel{thm:test_fr}
        % Notice the use of \cref[de,...]
        Quelques lignes de preuves résultant \cref[de, used by]{lem:result_fr} et des équations \labelcref[used by and on]{eq:Euler_fr}, \labelcref[used by]{eq:1_fr,eq:2_fr}.
    \end{proof}

    \begin{equation}\label{eq:Euler_fr}
        \mathrm{e}^{\mathrm{i}\pi} + 1 = 0.
    \end{equation}
    \begin{align}
        1 &= 0 + 1 \label{eq:1_fr}\\
        2 &= 1 + 1 \label{eq:2_fr}
    \end{align}
\end{adjustwidth}

\subsection{Example in German}

\UseLanguage{German}

% \SetUsedOnMessageText{Erscheint auf~#1.}
% \SetUsedByMessageText{Erscheint #1.}
% \SetUsedByAndOnMessageText{Erscheint #1 auf~#2.}
% \SetForwardReferenceRefForm{\crefthe[in,dat.]}
% \SetForwardReferencePagerefForm{\cpagerefthe[noun]}

\vspace{-1.5\baselineskip}
\setlength{\marginparwidth}{24em}
\setlength{\marginparsep}{-18em}
\begin{adjustwidth}{}{20em}
    \begin{lemma}\label{lem:result_de}
        Einige vorläufige Ergebnisse (siehe Gleichung \labelcref[used by]{eq:1_de}).
    \end{lemma}

    \begin{theorem}\label{thm:test_de}
        Text.
    \end{theorem}
    \begin{proof}\restorelabel{thm:test_de}
        % Notice the use of \cref[die, akk.,...]
        Einige Beweise, \cref[die, akk., used by]{lem:result_de} und die Gleichungen \labelcref[used by and on]{eq:Euler_de}, \labelcref[used by]{eq:1_de,eq:2_de}.
    \end{proof}

    \begin{equation}\label{eq:Euler_de}
        \mathrm{e}^{\mathrm{i}\pi} + 1 = 0.
    \end{equation}
    \begin{align}
        1 &= 0 + 1 \label{eq:1_de}\\
        2 &= 1 + 1 \label{eq:2_de}
    \end{align}
\end{adjustwidth}

\subsection{Example in Italian}

\UseLanguage{Italian}

\vspace{-1.5\baselineskip}
\setlength{\marginparwidth}{24em}
\setlength{\marginparsep}{-18em}
\begin{adjustwidth}{}{20em}
    \begin{lemma}\label{lem:result_it}
      Alcuni risultati preliminari (vedi l'equazione \labelcref[used by]{eq:1_it}).
    \end{lemma}

    \begin{theorem}\label{thm:test_it}
        Testo.
    \end{theorem}
    \begin{proof}\restorelabel{thm:test_it}
        % Notice the use of \cref[da,...]
        Alcune linee di prova derivanti \cref[da, used by]{lem:result_it} e dalle equazioni \labelcref[used by and on]{eq:Euler_it}, \labelcref[used by]{eq:1_it,eq:2_it}.
    \end{proof}

    \begin{equation}\label{eq:Euler_it}
        \mathrm{e}^{\mathrm{i}\pi} + 1 = 0.
    \end{equation}
    \begin{align}
        1 &= 0 + 1 \label{eq:1_it}\\
        2 &= 1 + 1 \label{eq:2_it}
    \end{align}
\end{adjustwidth}

\subsection{Example in Spanish}

\UseLanguage{Spanish}

\vspace{-1.5\baselineskip}
\setlength{\marginparwidth}{24em}
\setlength{\marginparsep}{-18em}
\begin{adjustwidth}{}{20em}
    \begin{lemma}\label{lem:result_es}
      Algunos resultados preliminares (véase la ecuación \labelcref[used by]{eq:1_es}).
    \end{lemma}

    \begin{theorem}\label{thm:test_es}
        Texto.
    \end{theorem}
    \begin{proof}\restorelabel{thm:test_es}
        % Notice the use of \cref[de,...]
        Algunas líneas de demostración resultantes \cref[de, used by]{lem:result_es} y las ecuaciones \labelcref[used by and on]{eq:Euler_es}, \labelcref[used by]{eq:1_es,eq:2_es}.
    \end{proof}

    \begin{equation}\label{eq:Euler_es}
        \mathrm{e}^{\mathrm{i}\pi} + 1 = 0.
    \end{equation}
    \begin{align}
        1 &= 0 + 1 \label{eq:1_es}\\
        2 &= 1 + 1 \label{eq:2_es}
    \end{align}
\end{adjustwidth}

\UseLanguage{English}

\subsection{Regarding the customization for languages with declensions}

If you wish to change the content of the messages in such cases, you might need to be careful about the definite article and declension involved. For example, in German, if you wish to change the text to \textquote{Erscheint in ... auf ...} (\emph{Appears in ... on ...}), you need to write:
\begin{code}
\SetUsedOnMessageText{Erscheint auf~#1.}
\SetUsedByMessageText{Erscheint #1.}
\SetUsedByAndOnMessageText{Erscheint #1 auf~#2.}
\SetForwardReferenceRefForm{\crefthe[in,(*\commandoption{dat.}*)]}
\SetForwardReferencePagerefForm{\cpagerefthe[(*\commandoption{noun}*)]}
\end{code}
which set the \lstinline|\cref| command to use the definite article \textquote{in} and the declension \textquote{Dativ} (via the command option \commandoption{dat.}), and hide the definite article when using \lstinline|\cpageref| (via the command option \commandoption{noun}). For more detailed usage of these commands, please refer to the documentation of the package \textsf{crefthe}.


\end{document}