summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/cleveref-forward/demo/cleveref-forward-demo.tex
blob: fec042a3a6914c3ac76a241b0071ff49f7c99c6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
\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{Demonstration on the usage and configuration of \textquote{\textsf{cleveref-forward}}}
\author{}
\date{\today[only-year-month]}

\begin{document}

\maketitle

\section{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{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} 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}

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,dat.]}
\SetForwardReferencePagerefForm{\cpagerefthe[noun]}
\end{code}
which set the \lstinline|\cref| command to use the definite article \textquote{in} and the declension \textquote{Dativ}, and hide the definite article when using \lstinline|\cpageref|.


\end{document}