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.

For executing code after the end of a parameterless macro there are \ltx@GlobalAppendToMacro and \ltx@LocalAppendToMacro from the ltxcmds-package.

For executing code after the end of an environment there is \AfterEndEnvironment from the etoolbox-package, and without the e-TeX extension one can use \renewenvironment for this.

But how can one "append" code after a label of an \item? \item has one (optional) parameter, thus \ltx@GlobalAppendToMacro and \ltx@LocalAppendToMacro cannot be used. At \end{description} would be too late.

MWE:

\documentclass{article}
\begin{document}
\begin{description}
 \item[Label\ReplaceMe{\textbf{OK}}] Text.
 \item[Label\ReplaceMe{\textbf{abc}}] Text.
 \item[Label] Text.
 \item Item without label.
\end{description}
\end{document}

The

\item[Label\ReplaceMe{\textbf{OK}}] Text.

should "expand" to

\item[Label]\textbf{OK} Text.

and the others accordingly. \item itself (or macros called by it) as found in

File A: ltlists.dtx Date: 2002/10/28 Version v1.0s (source 2e, 2011/06/27)

contains \box, \hbox,... The label text is thus "boxed", and I want to "free" a part of it. With "boxed" I do not want to indicate that there would be any rectangular line around "Label".

Looking at the answer provided by Werner, would it be sufficient to use the following code?

\documentclass{article}
\usepackage{letltxmacro}
\makeatletter
\newcommand*{\ReplaceMe}[1]{\gdef\saved{#1}}
\ReplaceMe{}
\LetLtxMacro{\@olditem}{\@item}%
\newenvironment{desc}%
  {\renewcommand*{\@item}[1][]{% \begin{desc}
     \@olditem[##1]\saved\gdef\saved{}%
    }%
   \begin{description}}%
  {\end{description}}% \end{desc}
\makeatother

\begin{document}
\noindent Here is some text.
\begin{desc}
 \item[Label\ReplaceMe{\textbf{OK}}] Text.
 \item[Label\ReplaceMe{\textbf{abc}}] Text.
 \item[Label] Text.
 \item Item without label.
\end{desc}
Here is some more text.
\begin{description}
 \item[Label\ReplaceMe{\textbf{OK}}] Text.
 \item[Label\ReplaceMe{\textbf{abc}}] Text.
 \item[Label] Text.
 \item Item without label.
\end{description}
\end{document}
share|improve this question
1  
Could you provide an MWE that also includes your "boxed" item label, as well as an example of your "manual" specification that clearly shows what you're after? The reason is that certain packages or definitions may be conflicting to solution techniques. So, knowing this beforehand may help in finding solutions. –  Werner Nov 18 '11 at 19:19
add comment

1 Answer 1

up vote 2 down vote accepted

Here is one option that you could use. The \ReplaceMe macro is constructed to use a toggle (true/false boolean variable) and store the contents in a box that can be retrieved later. Additionally, the regular \item macro (and associated \@item) is modified to box the label (using \fbox) and process the saved contents of \ReplaceMe.

Heeding @egreg's suggestion, an entirely new description environment desc is defined to restrict the scope of the \@item redefinition and keep it local:

enter image description here

\documentclass{article}
\usepackage{letltxmacro}% http://ctan.org/pkg/letltxmacro
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newsavebox{\ReplaceMeBox}% Store \item's \ReplaceMe
\newtoggle{ReplaceMe}% \ReplaceMe was used/not
\newcommand*{\ReplaceMe}[1]{%
  \global\toggletrue{ReplaceMe}% \ReplaceMe was used
  \global\sbox{\ReplaceMeBox}{#1}% Store contents in \ReplaceMeBox
}
\makeatletter
\LetLtxMacro{\@olditem}{\@item}%
\newenvironment{desc}%
  {\renewcommand*{\@item}[1][]{% \begin{desc}
   \@olditem[\fbox{##1}]\iftoggle{ReplaceMe}{\usebox{\ReplaceMeBox}\global\togglefalse{ReplaceMe}}{}}%
   \begin{description}}
  {\end{description}}% \end{desc}
\begin{document}
\noindent Here is some text.
\begin{desc}
 \item[Label\ReplaceMe{\textbf{OK}}] Text.
 \item[Label\ReplaceMe{\textbf{abc}}] Text.
 \item[Label] Text.
\end{desc}
Here is some more text.
\begin{description}
 \item[Label\ReplaceMe{\textbf{OK}}] Text.
 \item[Label\ReplaceMe{\textbf{abc}}] Text.
 \item[Label] Text.
\end{description}
\end{document}​

etoolbox provides the toggle mechanism, while letltxmacro was used as a precaution to handle optional argument copying across macros. To cross scope boundaries when switching toggles and saving boxes, \global was used.

share|improve this answer
1  
It's not a good idea to modify globally \@item; this should be done locally by defining a wrapper environment. –  egreg Nov 18 '11 at 20:38
    
@egreg: True. I'll update this momentarily. –  Werner Nov 18 '11 at 20:41
    
Sorry for having to leave yesterday evening. Thanks for your answer, it does work. Nevertheless I still have a question whether it would not be possible to do it "easier". Edited the question again. (Sorry for not being able to state it right in the first place.) I'll delete my obsolete comments to the question now (keeping this a little bit more "clean".) +1 and accepted your answer, of course. –  Stephen Nov 19 '11 at 15:48
    
@Stephen: Sure, if it works for you. From this discussion I hope it is clear how important it is to be clear in your problem description and provide as much detail as possible. "Boxed", as is now evident, has many meanings. As such, an MWE or a graphic/image is always a good idea. –  Werner Nov 19 '11 at 16:08
add comment

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.