9

I have a question. I have list which each item contain same text.

For example:

1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. SAMETEXT
2. Fusce blandit, orci vel cursus mollis, elit dui tristique purus, eget dapibus leo neque id mi. SAMETEXT
3. Quisque vehicula consequat elit in mattis. SAMETEXT

Each item contain "SAMETEXT". Is there any way to auto-display this text?

BlaBlaBla
  • 155
  • 4

4 Answers4

5

A method with patching, using a specialized enumerate environment, to prevent 'polution' for the patched \item command.

\documentclass{article}

\usepackage{enumitem}
\usepackage{multicol}
\usepackage{xpatch}

\newcommand{\TextToAppend}{\textbf{Don't do this at home}}

\newlist{dontdothislist}{enumerate}{1}
\setlist[dontdothislist,1]{label={\arabic*.}}


\xapptocmd{\dontdothislist}{%
\xpretocmd{\enddontdothislist}{\ifnumgreater{\value{dontdothislisti}}{0}{\TextToAppend}{}}{\typeout{Yes}}{\typeout{Nope}}
\xpretocmd{\item}{\ifnumgreater{\value{dontdothislisti}}{0}{\TextToAppend}{}}{\typeout{Yes}}{\typeout{Nope}}
}{}{}


\begin{document}

\begin{multicols}{2}
\begin{enumerate}
\item Bla
\item Blo
\item Blu
\item Ble
\item Bli
\end{enumerate}

\begin{dontdothislist}
\item Bla
\item Blo
\item Blu
\item Ble
\item Bli Blu Blo Bla Ble
\end{dontdothislist}


\end{multicols}

\end{document}

enter image description here

  • There might be issues with nested lists, perhaps ... did not test this –  Jul 07 '15 at 11:05
  • Can the space between the appended item and the text be removed? For example, 1. Bla Don't do this at home, to be 1. BlaDon't do this at home – user Aug 29 '17 at 13:35
  • @user: Why should one want to have this space removed? –  Aug 29 '17 at 14:02
  • It is because I need to append a dot. So a space before it would be not right. I have two list implementations, on one of them I need a dot appended to each item. But on the other I do not. Noticed both lists contains the same items. This way I declare \newcommand{\listcontents}[1]{\item first \item second} and call each one of the my implementations as begin{listwithdot}\listcontents\end{listwithdot} and on the other place I need the list items without a dot, I call the other list implementation begin{listwithoutdot}\listcontents\end{listwithoutdot} – user Aug 29 '17 at 15:58
  • @user: I fear, I can't help you here –  Aug 30 '17 at 20:58
4

Unless you just mean something very simple like this, rather than patching every list

\documentclass{article}

\newcommand{\sametext}{The world is your oyster}

\begin{document}

\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit. \sametext
\item Fusce blandit, orci vel cursus mollis, elit dui tristique purus, eget dapibus leo neque id mi. \sametext
\item Quisque vehicula consequat elit in mattis. \sametext
\end{enumerate}

\renewcommand{\sametext}{The world is your kitchen}

\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit. \sametext
\item Fusce blandit, orci vel cursus mollis, elit dui tristique purus, eget dapibus leo neque id mi. \sametext
\item Quisque vehicula consequat elit in mattis. \sametext
\end{enumerate}

\end{document}
3

It is also possible to patch the \item macro by hand. In this case I delimited the patched \item macro to collect all items, while inserting the desired text (declared by \everyitem) everytime, into a token register and then print it with the use of a plain enumerate environment:

\documentclass{article}

\makeatletter
\let\ltx@item\item
\newtoks\EI@toks
\newtoks\EI@items
\def\EI@item{\begingroup\catcode`\^^M=12 \EI@item@}
\bgroup\catcode`\^^M=12 %
  \gdef\EI@item@#1^^M{%
    \global\EI@items=\expandafter{\the\EI@items\ltx@item#1 \the\EI@toks}%
    \endgroup}%
\egroup
\newcommand{\everyitem}[1]{\global\EI@toks{#1}}

\newenvironment{sametextlist}{%
  \let\item\EI@item
}{%
  \begin{enumerate}
    \the\EI@items
  \end{enumerate}
}
\makeatother

\parindent0em
\everyitem{SAMETEXT}

\begin{document}
The \verb+sametextlist+ environment will print the text declared in the \verb+\everyitem+ macro after every use of the \verb+\item+ macro:

\begin{sametextlist}
  \item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  \item Fusce blandit, orci vel cursus mollis, elit dui tristique purus, eget dapibus leo neque id mi.
  \item Quisque vehicula consequat elit in mattis.
\end{sametextlist}

The usage of the \verb+\item+ macro isn't affected in the other environments:

\begin{enumerate}
  \item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  \item Fusce blandit, orci vel cursus mollis, elit dui tristique purus, eget dapibus leo neque id mi.
  \item Quisque vehicula consequat elit in mattis.
\end{enumerate}
\end{document}

output

Ruben
  • 13,448
3

Here, I define the environment renumerate, which takes as a mandatory argument the repeated text. In my MWE, I invoke enumerate to show how redefinitions of \item do not extend outside of the renumerate environment. No packages necessary!

\documentclass{article}
\let\svitem\item
\newcommand\newitem{\savetext\svitem}%
\newenvironment{renumerate}[1]
{%
  \def\savetext{#1}%
  \renewcommand\item{\let\item\newitem\svitem}%
  \begin{enumerate}%
}
{\savetext\end{enumerate}}
\begin{document}
\begin{renumerate}{My repeated text}
\item doe.
\item reh.
\item mi.
\end{renumerate}
\begin{enumerate}
\item doe.
\item reh.
\item mi.
\end{enumerate}
\end{document}

enter image description here

Nesting renumerate works but may not give quite what is expected:

\documentclass{article}
\let\svitem\item
\newcommand\newitem{\savetext\svitem}
\let\savetext\relax
\newenvironment{renumerate}[1]
{%
  \def\savetext{#1}%
  \renewcommand\item{\let\item\newitem\svitem}%
  \begin{enumerate}%
}
{\savetext\end{enumerate}}
\begin{document}
\begin{renumerate}{My repeated text}
\item doe.
\item reh.
  \begin{renumerate}{\textbf{Now for something different!}}
  \item X.
  \item Y.
  \item Z.
  \end{renumerate}
\item mi.
\end{renumerate}
\end{document}

enter image description here