Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reimplementing \item

0 views
Skip to first unread message

Flavio Costa

unread,
Oct 28, 2009, 6:32:37 PM10/28/09
to
Hi,

I want to write something like this:

\begin{itemize}
\item 500 rabbits
\optionalitem 400 dogs
\item 1000 carrots
\end{itemize}

I tried defining the new macro as follows:

\def\optionalitem#1{\item #1 [optional]}

It obviously do not work as expected, because only the number 400 is
taken as #1. How to make the macro to use everything until the end of
the line (or actually until the next \item) as the argument?

Thanks,

Flavio Costa

Lars Madsen

unread,
Oct 28, 2009, 6:56:24 PM10/28/09
to

what exactly are you trying to achive? It is not clear from your example
what it is you want to end up with

what should \optionalitem do that is different than \item?

/daleif

Flavio Costa

unread,
Oct 28, 2009, 7:09:31 PM10/28/09
to
Hi Lars,

I just want to append "[optional]" after the item text, as the macro
definition shows.

Flavio Costa

Scott Pakin

unread,
Oct 28, 2009, 7:29:26 PM10/28/09
to

If you don't mind following each \item line with a blank line, you can
use the following:

\def\optionalitem#1\par{\item #1 [optional]}

I admit I don't know how to bound #1 by the end-of-line character
instead of by \par. Maybe one of the local gurus can provide a clue.

-- Scott

Lars Madsen

unread,
Oct 28, 2009, 7:36:41 PM10/28/09
to

ok, because \item it self takes and optional argument, so it was a bit
confusing.

I'd do what Scott is suggesting.

/daleif

Flavio Costa

unread,
Oct 28, 2009, 11:44:38 PM10/28/09
to
Hi Scott and Lars,

Probably looking for the end-of-line character should require a tricky
code. As far as I could inspect \item's code, it seems to look for the
next \item or the end of the environment, but it's also too
complicated to get into it.

I think I'll end up doing this:

\begin{itemize}
\item 500 rabbits

\item \optional{400 dogs}


\item 1000 carrots
\end{itemize}

That's not what I was looking for, but it seems it's not worth the
effort looking for something more sophisticated.

Thanks for the tips,

Flavio Costa

unbonpetit

unread,
Oct 29, 2009, 4:02:28 AM10/29/09
to
Flavio Costa a écrit :

> I want to write something like this:
>
> \begin{itemize}
> \item 500 rabbits
> \optionalitem 400 dogs
> \item 1000 carrots
> \end{itemize}
>
> I tried defining the new macro as follows:
>
> \def\optionalitem#1{\item #1 [optional]}
Try this:

\documentclass{article}
\makeatletter
\newcommand\Ifinstr@[2]{%
\def\Ifinstr@i##1#2##2\@nil{%
\ifx&##2&\expandafter\@secondoftwo
\else\expandafter\@firstoftwo
\fi}%
\Ifinstr@i#1\@@nil#2\@nil}
\def\optionalitem#1\end{\Ifinstr@{#1}\item
{\optionalitem@#1\end}{\item#1 [optional]\end}}
\def\optionalitem@#1\item#2\end{\item#1 [optional]\item#2\end}
\makeatother
\begin{document}


\begin{itemize}
\item 500 rabbits
\optionalitem 400 dogs
\item 1000 carrots

\optionalitem 800 cats
\end{itemize}
\end{document}

Dan Luecking

unread,
Oct 29, 2009, 6:36:30 PM10/29/09
to
On Wed, 28 Oct 2009 17:29:26 -0600, Scott Pakin <scot...@pakin.org>
wrote:

This all seems a little silly. Why is
\optionalitem Text text
preferable to
\item Text text [optional]
as an input template?

One cannot (apparently) assume the the text is only one
line. Since an optional item may be last or followed by
another \optional item, one also cannot assume that \item
terminates the text.

One could (probably) have \optionalitem redefine \par,
so that it places the " [optional]" text and then restores
its previous meaning.

But it seems not at all worth the trouble.


Dan
To reply by email, change LookInSig to luecking

Peter Flynn

unread,
Oct 29, 2009, 7:29:42 PM10/29/09
to

This is why any document which requires this level of logic control
should be stored and maintained in XML, where you have the facilities
for flagging things like optionality.

You can then transform the document to LaTeX syntax when PDF is needed,
and add your business logic and effectivities in the transformation
process (eg XSLT).

///Peter

Flavio Costa

unread,
Oct 29, 2009, 10:57:26 PM10/29/09
to
First of all, I would like to thank unbonpetit for his solution, it's
tricky but it works as expected. A nice LaTeX exercise.

It's not really silly and Peter actually understands why. If you type
"\item Text text [optional]" and, at some time, you want to change
"[optional]" by something else (e.g. an icon), you'll need to do a
text replace with an editor application. This is error prone and it
does not ensure a consistent display of optional items - a possible
mistake would be to type "optional" sometimes between parenthesis,
sometimes between square brackets. It wouldn't happen if you keep this
inside a clean macro.

As Peter suggests, this kind of semantically rich structure may be
better represented in another format like XML rather than TeX.
However, working with alien technologies like XSLT adds complexity
that may be greater than the complexity of developing a pure-TeX
solution, so your mileage may vary.

Best regards,

Flavio Costa

Ulrike Fischer

unread,
Oct 30, 2009, 4:16:07 AM10/30/09
to
Am Thu, 29 Oct 2009 19:57:26 -0700 (PDT) schrieb Flavio Costa:

> It's not really silly and Peter actually understands why. If you type
> "\item Text text [optional]" and, at some time, you want to change
> "[optional]" by something else (e.g. an icon), you'll need to do a
> text replace with an editor application.

Then use \item Text \optionalmarker.

> As Peter suggests, this kind of semantically rich structure may be
> better represented in another format like XML rather than TeX.

Or \begin{optional}\item Text \end{optional} if you want a more
XML-like sematic. Redefining \item is problematic as it is only a
start "tag". Transformed in xml you have something like
"<item bullet /> Text" so trying to use the item tag to manipulate
the text looks wrong.

--
Ulrike Fischer

Dan Luecking

unread,
Oct 30, 2009, 1:19:16 PM10/30/09
to
On Thu, 29 Oct 2009 19:57:26 -0700 (PDT), Flavio Costa
<flavio....@gmail.com> wrote:

>First of all, I would like to thank unbonpetit for his solution, it's
>tricky but it works as expected. A nice LaTeX exercise.
>
>It's not really silly and Peter actually understands why. If you type
>"\item Text text [optional]" and, at some time, you want to change
>"[optional]" by something else (e.g. an icon), you'll need to do a
>text replace with an editor application. This is error prone and it
>does not ensure a consistent display of optional items - a possible
>mistake would be to type "optional" sometimes between parenthesis,
>sometimes between square brackets. It wouldn't happen if you keep this
>inside a clean macro.

If you want the ability to change "[optional]" to something
else (a possibility not previously mentioned) then put it
inside a macro, but not at the beginning of the item:

\item Text text \optional

Where you can (re)define \optional as you wish. Or, if you
want a more XML-like markup:

\optionalitem Text text \endoptionalitem

Then you could put the "[optional]" (or the icon or whatever)
at the beginning or the end.

Neither of these requires fancy tricks (which I still think
are a bit silly).

Flavio Costa

unread,
Oct 30, 2009, 9:01:21 PM10/30/09
to
Using something like \def\optionalmarker{[optional]} is not the same,
because it doesn't allow you to eventually have the marker before the
item text, or to make optional items appear in italics.

I agree, for the reasons exposed, that redefining \item generally does
not work well. Something like an environment or \optionalitem ...
\endoptionalitem is much nicer for this purpose.

My use for this is to typeset a cookbook, so I know that optional
items will be a short, single line text. A special kind of \item
actually looks good in the source code, but wrapping some items in a
command or environment is OK too.

Flavio Costa

0 new messages