2

I have a CSV table containing some numbers in percent with the percent char explictily written in the CSV data (that can’t be changes). Is there a (LaTeX3) way to strip the percent char and preceding space to only print the number (and use ist in \num)?

\documentclass{article}
\begin{filecontents*}{\jobname.csv}
a;b;5 %
c;d;87,58 %
e;f;100,0 %
\end{filecontents*}

\usepackage{csvsimple}

\begin{document}
\noindent
\csvreader[no head, respect percent, separator=semicolon]{\jobname.csv}{}{%
   percent of \csvcoli (\csvcoliii)\\
}
\end{document}

I already got half the way and got rid of the percent char, however I’m unable to remove the space as well …

\documentclass{article}
\begin{filecontents*}{\jobname.csv}
a;b;5 %
c;d;87,58 %
e;f;100,0 %
\end{filecontents*}

\usepackage{xparse,csvsimple}

\ExplSyntaxOn
\group_begin:
\catcode`\%=12
   \cs_new:Nn \scores_strip_percent_and_use:n {
      \tl_set:Nx \l_tmpa_tl {#1}
      \tl_show:N \l_tmpa_tl
      \tl_replace_once:Nnn \l_tmpa_tl { % } { }
      \tl_use:N \l_tmpa_tl
   }
\group_end:
\NewDocumentCommand { \printCSV } { } {
   \csvreader[no~head, respect~percent, separator=semicolon]{\jobname.csv}{}{
      percent~of~\csvcoli
      \c_space_tl
      (\scores_strip_percent_and_use:n { \csvcoliii }) \\
   }
}
\ExplSyntaxOff

\begin{document}
\noindent\printCSV
\end{document}
  • 1
    can't you set the catcode of % to 9 (ignore)? – Ulrike Fischer May 19 at 9:35
  • @UlrikeFischer: Hi, this is possible I guess, but would help for the space problem, would it (see also my comment toe gras answer). – Tobi May 19 at 9:55
  • Normally I wouldn't expect spaces to matter if used in a \num. E.g. \num{123.4}x \num{123.4 }x \num{123.4~}x works as expected. – Ulrike Fischer May 19 at 10:00
  • @UlrikeFischer, then this should work too :-) – Tobi May 19 at 10:24
2

With \regex_replace_once:nnN it's easy: \s*\%\Z means “a sequence of spaces followed by % at the end of the token list.

\documentclass{article}
\begin{filecontents*}{\jobname.csv}
a;b;5 %
c;d;87,58 %
e;f;100,0 %
\end{filecontents*}

\usepackage{xparse,csvsimple}

\ExplSyntaxOn

\NewDocumentCommand{\striptrailingpercent}{m}
 {
  \tl_set:Nx \l_tmpa_tl { #1 }
  \regex_replace_once:nnN { \s* \% \Z } {} \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
 }

\ExplSyntaxOff

\NewDocumentCommand {\printCSV}{}{%
   \csvreader[no head, respect percent, separator=semicolon]{\jobname.csv}{}{%
     \noindent percent of \csvcoli\ (\striptrailingpercent{\csvcoliii}) \par
   }
}

\begin{document}

\printCSV

\end{document}

enter image description here

You can feed the output to \num:

\documentclass{article}
\begin{filecontents*}{\jobname.csv}
a;b;5 %
c;d;87,58 %
e;f;100,0 %
\end{filecontents*}

\usepackage{xparse,csvsimple,siunitx}

\sisetup{group-separator=.} % just to show that \num has effect

\ExplSyntaxOn

\NewDocumentCommand{\striptrailingpercent}{m}
 {
  \tl_set:Nx \l_tmpa_tl { #1 }
  \regex_replace_once:nnN { \s* \% \Z } {} \l_tmpa_tl
  \num{\tl_use:N \l_tmpa_tl}
 }

\ExplSyntaxOff

\NewDocumentCommand {\printCSV}{}{%
   \csvreader[no head, respect percent, separator=semicolon]{\jobname.csv}{}{%
     \noindent percent of \csvcoli\ (\striptrailingpercent{\csvcoliii}) \par
   }
}

\begin{document}

\printCSV

\end{document}

enter image description here

  • Thanks, that works. And I just found out that my original data contained a no-break space which therefor wasn’t found in my tests … – Tobi May 19 at 9:54

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.