8

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?

8

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

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.