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.

What do I pass to \SplitList to get it to split the string on a new line? I've tried \n and \r, but neither of those work.

The code I used was

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand {\dothings}
  { > { \SplitList { \r } } m }
  { \tl_map_inline:nn {#1} { \fbox{##1} } }
\ExplSyntaxOff


\begin{document} 
\dothings{
Takes several words as argument
Stuff
        meow
        meow
}   
\end{document} 
share|improve this question
    
Welcome to TeX.SE! It would be helpful if you posted an example of the code you've tried so far, along with an indication of which TeX distribution and format (e.g., pdflatex, xelatex, lualatex, context) you're using. –  Mico Jan 17 '12 at 0:17
    
@Mico: Where do I find the TeX distribution/format? –  howardh Jan 17 '12 at 0:18
    
The TeX distribution would be something like TeXLive (if so: which year -- 2011?, 2010?, ...), MikTeX (2.9?, 2.8?, ...), tetex, MacTeX (and which year?). This piece should show up, very near the top of the log file, every time you run TeX. The format is something like Plain TeX, LaTeX, XeTeX, XeLaTeX, etc. –  Mico Jan 17 '12 at 0:21
    
@Mico: "This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)" Is it in there? –  howardh Jan 17 '12 at 0:23
3  
You can't, with the normal category codes. You can use \SplitList{~} to split at spaces. When TeX finds an end-of-line it changes it into a space or a \par, if another follows immediately. –  egreg Jan 17 '12 at 0:23
show 2 more comments

1 Answer

up vote 4 down vote accepted

Normally, TeX automatically converts newlines to spaces (and collapses any sequence of spaces to a single space). Avoiding that requires you to change "category codes" of the new-line character before reading the input. Rather than doing it ourselves, we can use the (long) verbatim argument type +v of xparse. This will collect the argument of \dothings as characters, interpreting neither commands nor special characters. After that, we split at every ^^M (to provide that as the argument of \SplitList, I'm using \iow_char:N \^^M, which expands to that character).

Thus, in the body of the document commad, #1 denotes a token list whose items are each line of input. We loop over all the lines. Omit blank lines. Then do what you want to do, e.g., \fbox, using \tl_rescan:nn { } to go back from raw characters (that we collected without caring about commands and special characters) to tokens (commands and special chars and normal chars).

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\dothings}
  { > { \exp_args:Nx \SplitList { \iow_char:N \^^M } } +v }
  {
    \tl_map_inline:nn {#1}
      {
        \tl_if_blank:nF { ##1 } % ignore blank lines (first and last)
          {
            \tl_rescan:nn { } { \fbox{##1} }
            % or \fbox { \tl_rescan:nn { } {##1} }
          }
      }
  }
\ExplSyntaxOff
\begin{document}
\dothings{
Takes several words as argument
Stuff
    meow
    meow
}
\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.