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.

How can I convert every line to a list item automatically?

for example I want following become a list (without adding more characters):

\begin{foo}
Persian
French 
Arabic
Urdu
\end{foo}
  • Persian
  • French
  • Arabic
  • Urdu
share|improve this question
add comment

2 Answers

up vote 13 down vote accepted

If the input is exactly laid out as you show, with \begin{foo} and \end{foo} on lines by themselves, the following will do:

\documentclass{article}

\makeatletter
\newcommand{\checkForEnd}{\@ifnextchar\end{}{\item}}
\makeatother
\newenvironment{foo}
 {\par\obeylines
  \begingroup\lccode`~=`\^^M
  \lowercase{\endgroup\let~}\checkForEnd
  \begin{itemize}}
 {\end{itemize}}

\begin{document}
\begin{foo}
Persian
French
Arabic
Urdu
\end{foo}

\begin{itemize}
\item Persian
\item French
\item Arabic
\item Urdu
\end{itemize}
\end{document}

The result of the two environments will be the same. But the foo environment should not appear in the argument to a command.

You can of course modify the appearance of the itemize environment, with the help of the package enumitem. For instance

\usepackage{enumitem}

\newenvironment{foo}
 {\par\obeylines
  \begingroup\lccode`~=`\^^M
  \lowercase{\endgroup\let~}\checkForEnd
  \begin{itemize}[nosep]}
 {\end{itemize}}

will "kill" all vertical space, while noitemsep in place of nosep will remove the space between the items, leaving the separation of the environment from the text above and below it.

You can even do the setup for each foo environment with

\usepackage{enumitem}

\newenvironment{foo}[1][]
 {\par\obeylines
  \begingroup\lccode`~=`\^^M
  \lowercase{\endgroup\let~}\checkForEnd
  \begin{itemize}[#1]}
 {\end{itemize}}

so that the options can be passed as optional argument to foo; for instance,

\begin{foo}[nosep]
Persian
French
Arabic
Urdu
\end{foo}
share|improve this answer
1  
@Reza It builds a real itemize environment. –  egreg Sep 23 '12 at 14:31
    
After a year your code save the day again. thanks –  PHPst Oct 12 '13 at 6:22
add comment

Here is one option: Using \obeylines and an "itemize definition" for \everypar.

enter image description here

\documentclass{article}
\newenvironment{foo}
  {\obeylines\everypar{\textbullet\hspace{\labelsep}}}% \begin{foo}
  {}% \end{foo}
\begin{document}
\begin{foo}
Persian
French 
Arabic
Urdu
\end{foo}

\begin{itemize}
\item Persian
\item French 
\item Arabic
\item Urdu
\end{itemize}
\end{document}
share|improve this answer
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.