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 would like to create my own list environment with custom \item element, with a custom tag myitem, that takes an optional parameter (the 2,7,6 in my example). It should be a numbered list. I should be able to reference it.

This is basically what I want:

Proposed latex code:

Look at \ref{a}, and \ref{b}
\begin{mylist}
\myitem[2] \label{a} lorum ipsum lorum ipsum
\myitem[7] lorum ipsum etc.
\myitem[6] \label{b} lorum ipsum etc.
\end{mylist}

The output should be:

Look at myPrefix-2-1, and myPrefix-6-3
myPrefix-2-1 lorum ipsum lorum ipsum
myPrefix-7-2 lorum ipsum etc.
myPrefix-6-3 lorum ipsum etc.

Should I clean up the question a bit more?

So far with Tom and egreg's help, I have the following code:

\usepackage{enumitem}

\newlist{mylist}{enumerate}{1}
\setlist[mylist]{label*=RQ\arabic*:~,ref=RQ\arabic*}
\makeatletter
\newcommand\myitem[1][]{%
  \if\relax\detokenize{#1}\relax
    \item\relax
  \else
    \protected@edef\@currentlabel{RQ#1}%
    \item[RQ#1:~]
  \fi}
\makeatother

All I now need is for the label referencing to work as stated in my edited question.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

One way is to let enumitem do some job

\documentclass[a4paper]{article}
\usepackage{enumitem}

\newlist{mylist}{enumerate}{1}
\setlist[mylist]{label*=RQ\arabic*:~,ref=RQ\arabic*}
\makeatletter
\newcommand\myitem[1][]{%
  \if\relax\detokenize{#1}\relax
    \item\relax
  \else
    \protected@edef\@currentlabel{RQ#1}%
    \item[RQ#1:~]
  \fi}
\makeatother

\begin{document}
\begin{mylist}
\myitem\label{x} x
\myitem[3]\label{y} y
\myitem z
\end{mylist}
--\ref{x}--\ref{y}--
\end{document}

The complicated conditional is just to make correct references also for \myitem[x]. Some other code can be added for getting also hyperlinks.

Check enumitem's documentation for how to change the other list parameters (indentation, label width and so on).

Addition

To get the numbering scheme you're proposing, change the code into

\usepackage{enumitem}

\newlist{mylist*}{enumerate}{1}
\setlist[mylist*]{label=\mylistprefix-\mylistopt-\arabic*:~,
  ref=\mylistprefix-\mylistopt-\arabic*}
\makeatletter
\let\mylistopt\relax % to keep enumitem happy
\newcommand\myitem[1][]{%
  \if\relax\detokenize{#1}\relax
    \def\mylistopt{0}% no optional argument to \myitem
  \else
    \def\mylistopt{#1}% optional argument to \myitem
  \fi
    \item\relax}
\makeatother
\newenvironment{mylist}[1][myPrefix]
  {\def\mylistprefix{#1}\begin{mylist*}}
  {\end{mylist*}}

You call it as

\begin{mylist}
\myitem[2] \label{a} lorum ipsum lorum ipsum
\myitem[7] lorum ipsum etc.
\myitem[6] \label{b} lorum ipsum etc.
\myitem ll
\end{mylist}

or

\begin{mylist}[RQ]
\myitem[2] \label{a} lorum ipsum lorum ipsum
\myitem[7] lorum ipsum etc.
\myitem[6] \label{b} lorum ipsum etc.
\myitem ll
\end{mylist}

to get RQ as the prefix.

A different implementation

Suppose you want the numbering of items to be unique for the whole document, but that you also want to decide at some point from where to start numbering. A key-value interface is more suitable:

\documentclass[a4paper]{article}
\usepackage{enumitem,keyval}
\makeatletter
\define@key{vort3xlist}{prefix}{\def\mylistprefix{#1}}
\define@key{vort3xlist}{start}{\setcounter{mylistcnt}{#1}}
\makeatother

\newcounter{mylistcnt}
\newlist{mylist*}{enumerate}{1}
\setlist[mylist*]{label=\mylistprefix-\mylistopt-\arabic{mylistcnt}:~,
  ref=\mylistprefix-\mylistopt-\arabic{mylistcnt}}
\makeatletter
\let\mylistopt\relax
\newcommand\myitem[1][]{%
  \refstepcounter{mylistcnt}%
  \if\relax\detokenize{#1}\relax
    \def\mylistopt{0}%
  \else
    \def\mylistopt{#1}%
  \fi
    \item\relax}
\makeatother
\newenvironment{mylist}[1][]
  {\setkeys{vort3xlist}{prefix=RQ}%change here your default prefix
   \setkeys{vort3xlist}{#1}%
   \begin{mylist*}}
  {\end{mylist*}}

\begin{document}
Look at \ref{a}, and \ref{b}
\begin{mylist}[start=3,prefix=XYZ]
\myitem[2] \label{a} lorum ipsum lorum ipsum
\myitem[7] lorum ipsum etc.
\myitem[6] \label{b} lorum ipsum etc.
\myitem \label{x} ll
\end{mylist}
Here's a second list
\begin{mylist}
\myitem[2] \label{c} lorum ipsum lorum ipsum
\myitem[7] lorum ipsum etc.
\myitem[6] \label{d} lorum ipsum etc.
\myitem \label{y} ll
\end{mylist}
\ref{c}, \ref{d}

\ref{x} and \ref{y}
\end{document}

The keys accepted are prefix and start, as you can see in the first mylist.

share|improve this answer
    
This might not be the right place to do it, but could you perhaps explain some more on why each item in the conditional is used? Or give me a link I can read up on how you reached this piece of code? –  Vort3x Sep 16 '11 at 16:30
    
I would like \ref{x} /\ref{y} to show the reference as RQ-3-1, where the 1 is the enumerated counter. I tried using \arabic and \arabic*, but I have no idea what I'm doing, and struggling to find documentation on advanced topics as this. If the item was made without a parameter, it should be RQ-0-1. I would also like the list items to display as RQ-param-itemcounter, instead of RQparam. –  Vort3x Sep 16 '11 at 17:09
    
@Vort3x: it's not very clear what numbering scheme you have in mind. Maybe you can edit your question to make it more explicit. –  egreg Sep 16 '11 at 19:13
    
<>@egreg: I just totally reworked the question to make it cleaner and clearer. –  Vort3x Sep 16 '11 at 19:34
    
Btw how do you do the @Vort3x: ? with the ':' and ' ' –  Vort3x Sep 16 '11 at 19:39

You can quite easily define your own lists. Put that in an environment, define a custom item. Here you go (you don't need the geometry package, I just hate to waste too much space):

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}

\newcounter{itemnumber}

\newenvironment{mylist}[1]{% Prefix
\setcounter{itemnumber}{0}%
\begin{list}{\textbf{#1}}{}%
}{\end{list}}

\newcommand{\myentry}[2]{% Number, Text
\stepcounter{itemnumber}%
\item \textit{#1}: \theitemnumber\ #2%
}

\begin{document}

\begin{mylist}{myPrefix}
    \myentry{2}{lorem ipsum adin}
    \myentry{7}{lorem ipsum dwa}
    \myentry{6}{lorem ipsum tri}
\end{mylist}

\end{document}

enter image description here


Edit 1: For referencable entries you can use hyperrefs hyperlink and hypertarget:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{lipsum}
\usepackage[colorlinks]{hyperref}

\newcounter{itemnumber}

\newenvironment{mylist}[1]{% Prefix
\setcounter{itemnumber}{0}%
\begin{list}{\textbf{#1}}{}%
}{\end{list}}

\newcommand{\myentry}[2]{% Number, Text
\stepcounter{itemnumber}%
\item \textit{#1}: \theitemnumber\ #2%
}

\newcommand{\mylinkentry}[3]{% Number, Text, linklabel
\stepcounter{itemnumber}%
\item \textit{#1}: \theitemnumber\ \hypertarget{#3}{#2}%
}

\begin{document}

\hrule

\begin{mylist}{myPrefix}
    \myentry{2}{lorem ipsum adin}
    \myentry{7}{lorem ipsum dwa}
    \myentry{6}{lorem ipsum tri}
\end{mylist}

\hrule

\begin{mylist}{some numbers}
    \mylinkentry{13}{bad luck}{item.one}
    \mylinkentry{665}{neighbour of the beast}{item.two}
    \mylinkentry{1337}{deep magic wizard}{item.three}
\end{mylist}

\hrule

Many people believe number \hyperlink{item.one}{13} to be bad luck.\\
The code was still blinking on th screen: \hyperlink{item.two}{665...}\\
Some people just got \hyperlink{item.three}{a lot more skill} than others.

\end{document}

enter image description here

share|improve this answer
    
Is there a way that I can add referable labels to each of these items? –  Vort3x Sep 16 '11 at 8:07
    
The revised version now has a \mylinkentry command, which will add a \hypertaggetwith the label specified. If you just want \labels for later \ref and \pageref, you can simply replace \hypertarget{#3}{#2} with \label{#3}#2 –  Tom Bombadil Sep 16 '11 at 11:07

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.