3

I'd like to make a container in LaTeX in which I can store and append LaTeX-code for later use.

I have found many solutions to what I'm basically try to do (here for instance), but if they work, they stop working when you try to use it inside an enumerate environment.

To make sure the solution works in my case, I've made an example that uses all relevant features the container must be capable of. In this example \addtotraceabilitymatrixcontent should append #1 &#2\\ to the \traceabilitymatrixcontent container.

\documentclass{article}

\newcommand{\traceabilitymatrixcontent}{}
\newcommand{\addtotraceabilitymatrixcontent}[2]
{
    % something that safely appends {#1 &#2 \\} 
    % to \traceabilitymatrixcontent while keeping
    % the references intact.
}

\newcommand{\maketraceabilitymatrix}
{
    \begin{tabular}{ll}
        Requirement & Validation \\
        \traceabilitymatrixcontent
    \end{tabular}
}

\newcommand{\reqitem}[2]
{
    \item \label{req:#1} \addtotraceabilitymatrixcontent{\ref{req:#1}}{#2}
}

\begin{document}

    \maketraceabilitymatrix

    \begin{enumerate}
        \reqitem{1}{\ref{val:1}} Requirement one
        \reqitem{2}{\ref{val:2}, \ref{val:3}} Requirement two
        \reqitem{3}{\ref{val:2}, \ref{val:3}} Requirement three
    \end{enumerate}

    \begin{enumerate}
        \item \label{val:1} Test to validate requirement one
        \item \label{val:2} Test to validate requirement two and three partially
        \item \label{val:3} Test to validate requirement two and three partially
    \end{enumerate}
\end{document}
1

I might have something in the right direction. I had to figure this out to write a best practices document where I had to very clearly identify the best practices, be able to reference them, and list them.

The solution I have uses memoir, and defines a new environment which is numbered. I am pretty sure I got some of the code from the memoir manual and somewhere on here.

This code generates a series of main items with sub items, where a short description of the main item is stored for use in a table of items.

Noting that this is probably incomprehensible, here is a MWE...

\documentclass{memoir}

\usepackage{enumitem}

%% Custom list
% define the list of MyList's
\newcommand{\listMyListname}{List of My Items}
\newlistof{listofMyLists}{MyList}{\listMyListname}
% define a counter
\newcounter{MyListno}
\newcommand{\theMyList}{\arabic{MyListno}}    
% define the command that creates an MyList, which is indexed every time we call it
\newcommand{\MyList}[2]{%   
    \refstepcounter{MyListno} \protect\item #2 %
    \addcontentsline{MyList}{MyListno}{\protect\numberline{\theMyList}#1}
    \par
}
% set up the list
\newlistentry{MyListno}{MyList}{0}
% borrow formatting
\let\printMyListtitle\printtoctitle    
% define the environment in which we will embed the MyList
\newenvironment{MyListlist}{%
    \begin{list}{\underline{\textbf{MyList \theMyList:}}~}{}%
    }{%
    \end{list}%
}
% Reference an MyList
\newcommand{\refMyList}[1]{\hyperref[#1]{MyList \ref{#1}}}    
% CREAT SUB ITEMS
\newlistentry[MyListno]{SubList}{MySubList}{1} 
\renewcommand{\theSubList}{\theMyList\  \alph{SubList})}    
\newenvironment{MySubListlist}{%
    \begin{list}{\textbf{\theSubList}~}{}%
}{%
    \end{list}%
}
\newcommand{\MySubList}[1]{\refstepcounter{SubList} \item #1}    

\usepackage{hyperref}    

\begin{document}

\listofMyLists

\begin{MyListlist}
    \MyList{Item the first}{Thou shalt do x.\label{MyList:Dox}}
    \begin{MySubListlist}
    \MySubList{In the first sub case:
    \begin{itemize}
\item sub item 1.1
\item sub item 1.2
\end{itemize}}      
    \MySubList{In the second sub case:
        \begin{itemize}
        \item sub item 2.1.
        \item sub item 2.2.
        \end{itemize}}
    \MySubList{In the third sub case: 
        \begin{itemize}     
        \item sub item 3.1.
        \item sub item 3.2.
        \item sub item 3.3.
        \end{itemize}}
    \end{MySubListlist}
\end{MyListlist}

In \refMyList{MyList:Dox}, I said something.

\end{document}

You then end up with something that looks like this: enter image description here

You'll see there are the list items, a table of short descriptions of the list items, and ways to reference the list items. I suspect you could use a similar approach, possibly removing the sub items?

  • Unfortunately not really what I'm going for. The problem is not really to cross reference to a list. The problem is to use a certain macro from out a list. – tiM May 24 '13 at 10:55
  • @tiM: you might want to rework your question and give a mock-up of what you were looking for? – Andy Clifton May 24 '13 at 15:09
  • I just rewrote the question, thanks for the advice – tiM May 27 '13 at 14:27
1

I've found what I was looking for in the etoolbox package. I still don't know exactly what I'm doing but defining \addtotraceabilitymatrixcontent as follows did the trick:

\newcommand{\addtotraceabilitymatrixcontent}[2]
{
    \gappto{\traceabilitymatrixcontent}{#1 & #2\\}
}
  • You're appending information to the replacement text of \traceabilitymatrixcontent. It's indeed the right thing to do; the command \gappto differs from \appto in that its action is global and it's necessary because environments form a group. – egreg May 30 '13 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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