%! TEX program = xelatex
\documentclass[classical]{einfart}

\linenumbers

\usepackage{ProjLib}

%%================================
%% For typesetting code
%%================================
\usepackage{listings}
\definecolor{maintheme}{RGB}{70,130,180}
\definecolor{forestgreen}{RGB}{21,122,81}
\definecolor{lightergray}{gray}{0.99}
\lstset{language=[LaTeX]TeX,
    keywordstyle=\color{maintheme},
    basicstyle=\ttfamily,
    commentstyle=\color{forestgreen}\ttfamily,
    stringstyle=\rmfamily,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{lightergray},
    flexiblecolumns=true,
    escapeinside={(*}{*)},
    % numbers=left,
    numberstyle=\scriptsize, stepnumber=1, numbersep=5pt,
    % firstnumber=last,
}
\providecommand{\meta}[1]{$\langle${\normalfont\itshape#1}$\rangle$}
\lstset{moretexcs=%
    {
        cref,crefname,
        crefthe,ccref,crefthemark,
        selectlanguage,
    }
}
\lstnewenvironment{code}%
{\setstretch{1.07}\LocallyStopLineNumbers%
\setkeys{lst}{columns=fullflexible,keepspaces=true}%
}
{\ResumeLineNumbers\vspace{-.5\baselineskip}}
\lstnewenvironment{code*}%
{\setstretch{1.07}\LocallyStopLineNumbers%
\setkeys{lst}{numbers=left,columns=fullflexible,keepspaces=true}%
}
{\ResumeLineNumbers}

%%================================
%% 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 = 0.3cm,
        after skip = 0.3cm,
        top = 3mm,
        bottom = 3mm,
        title={\scshape\sffamily #1}]%
}{\end{tcolorbox}\ResumeLineNumbers}

%%================================
%% demo
%%================================
\newenvironment{demo}{%
    \LocallyStopLineNumbers%
    \begin{tcolorbox}[enhanced jigsaw,pad at break*=1mm,breakable,
        left=4mm,right=4mm,top=0.5mm,bottom=0mm,
        colback=gray!5!paper,boxrule=0pt,frame hidden,
        borderline west={1.5mm}{0mm}{gray!55!paper},arc=.7mm]%
}{\end{tcolorbox}\ResumeLineNumbers}


\newcommand{\crefthepackage}{\textsf{crefthe}}

\begin{document}

\title{\crefthepackage{} - cross referencing with proper definite articles}
\author{Jinwen XU}
\thanks{Corresponding to: \texttt{\crefthepackage{} 2021/11/05}}
\date{November 2021, in Paris}

\maketitle

\begin{abstract}
    \raggedleft
    The package \crefthepackage{} provides a command \lstinline|\crefthe| parallel to \textsf{cleveref}'s \lstinline|\cref| for handling definite articles properly (especially for the article contractions in some European languages).
\end{abstract}

\section{The motivation}

By default, with \textsf{cleveref}'s \lstinline|\cref| to reference theorem-like environments, the names do not contain definite articles. While this might be acceptable for English, it is certainly not good enough for languages such as French, Italian, Portuguese, Spanish, etc. -- in these cases there shall be grammatical errors and would give you a strong feeling that it is machine-generated.

However, even if we manually add the definite articles to the names, there would still be other problems. As an example, if we define the French names to be:

\begin{code}
\crefname{theorem}{le théorème}{les théorèmes}
\crefname{proposition}{la proposition}{les propositions}
\end{code}

then when one writes (which means ``\emph{We can deduce this from ...}'')

\begin{code}
On peut le déduire de \cref{thm1,thm2,prop3}.
\end{code}

the result would be:

\begin{demo}
    On peut le déduire \textbf{de les} théorèmes 1 et 2 et \textbf{la} proposition 3.
\end{demo}

which is wrong, as the correct result should be:

\begin{demo}
    On peut le déduire \textbf{des} théorèmes 1 et 2 et \textbf{de la} proposition 3.
\end{demo}

\lstinline|\cref| cannot handle such cases automatically --- that is when \lstinline|\crefthe| comes into play.


\section{The usage}

\subsection[How to load it]{How to load it\,?}

Simply load the package with:

\begin{code}
\usepackage{crefthe}
\end{code}

\begin{tip}
    \begin{itemize}
        \item Since \crefthepackage{} uses \textsf{cleveref} internally, it should usually be placed at the last of your preamble, and notably, after \textsf{varioref} and \textsf{hyperref}.
        \item To handle article contractions correctly, \lstinline|\crefthe| shall detect the current language, thus you need to use packages such as \textsf{babel} or \textsf{polyglossia} to set your languages, and use commands like \lstinline|\selectlanguage| to select them appropriately.
    \end{itemize}
\end{tip}

\subsection[How to use it]{How to use it\,?}

Then you can use the command \lstinline|\crefthe| as follows:
\begin{itemize}
    \item \lstinline|\crefthe[|\meta{prep}\lstinline|]{|\meta{labels}\lstinline|}|
    \begin{itemize}
        \item This will pass the preposition \meta{prep} to the definite articles that follows. Its behavior depends on the current language (for example, in Spanish, \meta{prep} is passed only to the first definite article, while in French it is passed to everyone).
    \end{itemize}
    \item \lstinline|\crefthe-[|\meta{prep}\lstinline|]{|\meta{labels}\lstinline|}| and \lstinline|\crefthe+[|\meta{prep}\lstinline|]{|\meta{labels}\lstinline|}|
    \begin{itemize}
        \item In case the automatic version does not meet your needs, here are two manual ones. The \verb|-| version passes the preposition \meta{prep} only to the first definite article, while the \verb|+| version passes \meta{prep} to every definite article.
    \end{itemize}
\end{itemize}

\begin{tip}
    There is also a stared version \lstinline|\crefthe*| for generating the same referencing text but without creating hyperlinks.
\end{tip}
 
However, before using it, you should first define the \lstinline|\crefname|s carefully. The definite article in \lstinline|\crefname|s needs to be marked manually using \lstinline|\crefthemark|, for example:

\begin{code}
\crefname{theorem}{\crefthemark{le} théorème}
                  {\crefthemark{les} théorèmes}
\end{code}


\section{Example}

Let us come back to the example at the beginning, now you can do this:

\begin{code}
\crefname{theorem}{\crefthemark{le} théorème}
                  {\crefthemark{les} théorèmes}
\crefname{proposition}{\crefthemark{la} proposition}
                      {\crefthemark{les} propositions}
\end{code}

And the sentence shall be written as:

\begin{code}
On peut le déduire \crefthe[de]{thm1,thm2,prop3}.
\end{code}

which would result in (provided that you have done \lstinline|\selectlanguage{french}|):

\begin{demo}
    On peut le déduire \textbf{des} théorèmes 1 et 2 et \textbf{de la} proposition 3.
\end{demo}

Voilà !


\section{Known issues}
\begin{itemize}
    \item \crefthepackage{} currently only works for French, Italian, Portuguese (both European and Brazilian) and Spanish, certainly more would be added to this list.
    \item The current mechanism does not work for German. However, the author has planed to adopt a more refined approach in later versions in order to support the various situations in German.
    \item In case that the initial letter of \meta{prep} is capitalized, \crefthepackage{} cannot yet handle the case changes automatically. However, this should be a rare occurrence.
    \item The names of theorem-like environments are not provided --- for the moment you need to define them all by yourself. However, users are encouraged to use the \ProjLib{} toolkit, which already handles everything for you.
\end{itemize}


\end{document}
