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.

I'm inserting graphics from data in a csv-file and due to lazy input I have to get rid of leading and trailing spaces. It works fine with the trimspaces package.

\documentclass{article}
\usepackage{expl3,graphicx}
\usepackage{trimspaces}
\begin{document}

\def\testA{example-image-a }
%\includegraphics{\testA} %error

\def\testB{ example-image-a}
%\includegraphics{\testB} %error

\makeatletter
\trim@spaces@in\testA
\includegraphics{\testA}

\trim@spaces@in\testB
\includegraphics{\testB}


\end{document}

By curiosity I tried to get this with expl3 and \tl_trim_spaces:n but didn't find a sensible way to store the content of the commands without the spaces in a command. Does someone know how to do it?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

Two ways. With the first method you use \trimspaces in front of the macro; with the second you can ‘normalize’ a macro to have leading and trailing spaces removed.

\documentclass{article}
\usepackage{xparse,graphicx}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\trimspaces}{m}
 {
  \tl_trim_spaces:V #1
 }
\cs_generate_variant:Nn \tl_trim_spaces:n { V }

\NewDocumentCommand{\normalize}{m}
 {
  \tl_trim_spaces:N #1
 }
\ExplSyntaxOff

\begin{document}

\def\testA{example-image-a }
\def\testB{ example-image-a}

\includegraphics[width=4cm]{\trimspaces\testA}

\includegraphics[width=4cm]{\trimspaces\testB}

\normalize{\testA}\normalize{\testB}

\includegraphics[width=4cm]{\testA}

\includegraphics[width=4cm]{\testB}

\end{document}

enter image description here

share|improve this answer
    
Ah I see. I hadn't understand that \tl_trim_spaces:N does the same as \trim@spaces@in, I thought it would leave things in the input stream like \tl_trim_spaces:n. And I hadn't thought about V type, I only tried x. –  Ulrike Fischer Dec 19 '13 at 10:53
    
@UlrikeFischer The description of \tl_trim_spaces:N could be clearer. –  egreg Dec 19 '13 at 10:56
    
I can look to improve the docs for a release likely next week: @UlrikeFischer was there something specific that was missing? –  Joseph Wright Dec 19 '13 at 11:23
1  
@JosephWright Maybe “Resets ⟨tl var⟩ by removing any leading and trailing explicit space characters (explicit tokens with character code 32 and category code 10) from its content.” –  egreg Dec 19 '13 at 11:26
    
@JosephWright: The main source of confusion was in my case the naming of the commands: Both \tl_trim_spaces:n and \tl_trim_spaces:N share the same command "body" \tl_trim_spaces and so I assumed that they are variants and do more or less the same only on different types of arguments and I transfered "leaves the result in the input stream" from the description of \tl_trim_spaces:n to the other command. –  Ulrike Fischer Dec 19 '13 at 15:30

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.