3

I'd like to associate a symbol to an \itemize environnement so that when I use\ref it prints the symbol. I'd also like to have the symbol chosen printed in the middle of the \itemize environnement. It kind of looks like a \tag in the \equation environnement but I get compilation error when I tried it...

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[francais]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{enumitem}
\usepackage{hyperref, nameref, cleveref}

\begin{document}

\begin{itemize} \tag{$\dagger$} \label{épée}

\item blalabla
\item blablabla
\item blablabla
\end{itemize}

blablabla cf \cref{épée}
\end{document}
WrabbitW
  • 251

1 Answers1

4

Following Werner's fine answer at How can one refer to a part of an equation?, I can make it work with hyperref, but not with cref. Perhaps someone who know the guts of cref can adapt (here, I just \let \cref to \ref).

The new environment is myitemize, and it takes an optional argument, in which the label can be specified with \ilabel. Of course, you must define all the associated symbols with your environment in advance, as you will see in my preamble.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[francais]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{nameref}
%\usepackage{cleveref}
\let\cref\ref
\usepackage[nopar]{lipsum}
\makeatletter
\expandafter\def\csname mysym1\endcsname{\dag}
\expandafter\def\csname mysym2\endcsname{\ddag}
\expandafter\def\csname mysym3\endcsname{*}
\expandafter\def\csname mysym4\endcsname{\&}    
\newcounter{itnum}
\newenvironment{myitemize}[1][]%
  {\relax\par\stepcounter{itnum}#1\hfil[\csname mysym\theitnum\endcsname]\hfil\itemize}%
  {\enditemize\ignorespaces}
\AtBeginDocument{\let\i@label\label}% https://tex.stackexchange.com/q/9939/5764
\newcommand{\ilabel}[1]{\def\@currentlabel{\csname mysym\theitnum\endcsname}\i@label{#1}}
\makeatother
\begin{document}
\lipsum[4]

\begin{myitemize}[\ilabel{épée}]
\item blalabla
\item blablabla
\item blablabla
\end{myitemize}
\lipsum[4]
blablabla cf \cref{épée}
\begin{myitemize}[\ilabel{ref2}]
\item blalabla
\item blablabla
\item blablabla
\end{myitemize}
\lipsum[4]
blablabla cf \cref{ref2}
\end{document}

enter image description here


To help the novice user learn a bit more about what is done, I am expanding the explanation.

\makeatletter...\makeatother - See What do \makeatletter and \makeatother do?. Basically, @ is a restricted character in user code, but its use in macro names can be enabled/disabled with these two commands. Since much of the TeX core and packages use this @ character in their macro names, the enabling must be done to allow modifications to those routines.

\expandafter\def\csname mysym1\endcsname - numbers are not allowed in macro names when using \def, so \def\mysym1{\dag} will not work, but that is what I want to achieve. The name \csname mysym1\endcsname is effectively equivalent to a \mysym1 syntax. However, saying \def\csname... would redefine \csname, which I don't want to do, and so \expandafter says "skip over the next thing (in this case \def) and expand what follows, namely \csname. It effectively is equivalent to the disallowed \def\mysym1{}, but follows the rules.

\newcounter{itnum} and \stepcounter{itnum} - creates (zeroes) and increments a LaTeX structure called a "counter", which is just an integer index. The current value can be accessed (as a text string) by placing \the before the counter name, in this case \theitnum.

\par - start a new paragraph

\relax - do nothing, but it doesn't "expand", and so something trying to expand the code leading up to it will stop expanding when it get to \relax. I put it here, so that nothing which followed could accidentally be taken as part of the optional argument.

\hfil is stretchable glue (\hfill is a stretchier version of \hfil). It can stretch its horizontal length between the items to its left and right. Think of it as a rubber band.

\@currentlabel and \i@label are the way LaTeX refers internally to a label, and how it creates a new one. It is the first of these command that I must modify to make this method work. Then I must call on the second of these, passing it the "new" label definition.

\AtBeginDocument{} - Says "don't do this until \begin{document}. The reason for this is that hyperref is busy redefining stuff all over the place, and so I don't want to do this until hyperref is fully done doing what initializations it wants to do.

\ilabel - Since a user can't use @ characters in their code normally, I create this macro to effectively invoke \i@label with the properly redefined label.

  • Hi! I'm sorry but it has only been two weeks since I started using Latex so I'm affraid I didn't get any of your code...It seems a bit too complicated for me (maybe too soon). Maybe you know an easier solution (using a different environnement?). Anyway thank you a lot for your answer I guess it took you some time to figure it out! – WrabbitW Mar 22 '14 at 20:32
  • @WrabbitW I understand you are learning, but it in this case, you don't yet have to fully digest it. You can copy the code in the preamble, between the \makeatletter and \makeatother directly into your own document preamble. Thereafter, you can mimic the usage that I had in the document itself. The only additions you will need to make to my preamble code are adding more lines of the form \expandafter\def\csname mysym4\endcsname{\&}, where the number 4 becomes 5, and the 5th symbol is added instead of \&. The lipsum stuff is just filler text. cref could remain an issue... – Steven B. Segletes Mar 22 '14 at 20:43
  • @WrabbitW Even if you still use cref, you could use this simultaneously, as long as you referred to the labels with \ref instead of \cref. Also, if you are using \cref, eliminate the \let\cref\ref line, which merely forced the letters \cref to call upon \ref instead. – Steven B. Segletes Mar 22 '14 at 20:45
  • Okay thanks a lot but I don't want to use something I don't understand... and there's too many commands I don't know about... I guess I'll just label the middle line of my \itemize (because in your code the dagger symbol appears on top of the list and I wanted it to be in the middle...) Sorry for bein a pain in the @@@.. – WrabbitW Mar 22 '14 at 20:56
  • @WrabbitW I will edit my answer to provide a more detailed explanation, to help you learn. – Steven B. Segletes Mar 22 '14 at 21:00
  • @WrabbitW Please see revision. – Steven B. Segletes Mar 22 '14 at 21:37
  • Thank you for this complete explanation of your code. I think I understood almost all of it, but I don't exactly get what \hfildoes... It does comlicate a lot the issue to have a different symbol in each new \itemize! So I will try to write something omiting this part, but tomorow cause it's getting late and i'm getting tired! Thanks again. – WrabbitW Mar 22 '14 at 22:04
  • @WrabbitW I made a different symbol for each myitemize, otherwise if two such calls used the same symbol, then referring to the symbol would be ambiguous as to which myitemize you wanted. I also realized that both fills can be changed to \hfil (but not both to \hfill). These commands don't print. Think of them as rubber bands attached to each margin, and whatever is between them (the label) gets suspended midway across the line, regardless of how long the line is. That is how TeX centers things automatically...with what is called "glue" and what I am calling a rubber band. – Steven B. Segletes Mar 23 '14 at 00:52
  • Ok so if I put the command \hfil[\csname mysym\theitnum\endcsname]\hfil after the \begin{itemize} I should get the symbol inside the environement right? – WrabbitW Mar 23 '14 at 10:21
  • Well it's not really working but I think I'll just do it like this. A last issue remains : when clicking on the internal link it leads me to the previous page (which is the first page of the subsection) and not to the list... – WrabbitW Mar 23 '14 at 10:33
  • Also I don't quite well get the point of using \newcounter{i@counter} because it doesn't appear after... Neither I get the line that follows where you define \ilabel – WrabbitW Mar 23 '14 at 10:53
  • @WrabbitW The i@counter appears to be extraneous. I guess I overlooked deleting it. – Steven B. Segletes Mar 24 '14 at 19:39