Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
2  
Can you post what you've tried? That will make it easier for us to help :) –  Sean Allred Mar 22 at 18:13
    
It's done thank you for your time :) –  WrabbitW Mar 22 at 19:01

1 Answer 1

up vote 4 down vote accepted

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}% http://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.

share|improve this answer
    
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 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 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 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 at 20:56
    
@WrabbitW I will edit my answer to provide a more detailed explanation, to help you learn. –  Steven B. Segletes Mar 22 at 21:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.