2

In this MWE, I need to suppress the output of comment written inside the cell entry. In other words, I need to make a placeholder for comments inside cells of my interest.

\RequirePackage{filecontents}
\begin{filecontents*}{sample.csv}
1 (comment to suppress) , 2 (another comment to suppress)
\end{filecontents*}

\documentclass{article}
\usepackage{datatool}
\DTLloaddb[noheader]{db}{sample.csv}

\begin{document}
    \DTLgetvalue{\firstvalue}{db}{1}{1}
    \DTLgetvalue{\secondvalue}{db}{1}{2}
    \firstvalue and \secondvalue
\end{document}
Diaa
  • 9,599
  • Are all the comments of the form <space>(<text>)? – egreg Dec 17 '16 at 18:53
  • @egreg It doesn't matter, I just need any opening and closing symbols to be recognized by datatool as a placeholder for comments. – Diaa Dec 17 '16 at 18:56

1 Answers1

1

Assuming your comments are of the form (<text>), you can do like this:

\begin{filecontents*}{\jobname.csv}
1 (comment to suppress), 2 (another comment to suppress), 3
\end{filecontents*}

\documentclass{article}
\usepackage{datatool}

\makeatletter
\newcommand{\decomment}[1]{%
  \expandafter\de@comment#1()\@nil{#1}%
}
\def\de@comment#1(#2)#3\@nil#4{%
  \def#4{#1}%
}
\makeatother

\DTLloaddb[noheader]{db}{\jobname.csv}

\begin{document}
\DTLgetvalue{\firstvalue}{db}{1}{1}
\DTLgetvalue{\secondvalue}{db}{1}{2}
\DTLgetvalue{\thirdvalue}{db}{1}{3}

\decomment{\firstvalue}\decomment{\secondvalue}\decomment{\thirdvalue}

\firstvalue{} and \secondvalue{} and \thirdvalue{}

\end{document}

Curiously enough, the first entry becomes 1(comment to suppress), so the space cannot be safely removed this way, without a more complex check.

An easier method is with l3regex:

\begin{filecontents*}{\jobname.csv}
1 (comment to suppress), 2 (another comment to suppress), 3
\end{filecontents*}

\documentclass{article}
\usepackage{datatool}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\decomment}{m}
 {
  \regex_replace_once:nnN { \s*\(.*\)\s*\Z } { } #1
 }
\ExplSyntaxOff

\DTLloaddb[noheader]{db}{\jobname.csv}

\begin{document}
\DTLgetvalue{\firstvalue}{db}{1}{1}
\DTLgetvalue{\secondvalue}{db}{1}{2}
\DTLgetvalue{\thirdvalue}{db}{1}{3}

\decomment{\firstvalue}\decomment{\secondvalue}\decomment{\thirdvalue}

\firstvalue{} and \secondvalue{} and \thirdvalue{}

\end{document}

The regular expression is “any number of spaces followed by (, by arbitrary tokens, by ), by spaces and the end of the token list”.

egreg
  • 1,121,712
  • Thanks for your answer, I will try it and get back to you in case I need some tweaks. Off-topic question: what is the difference between storing data in \jobname.csv and sample.csv? is it better to take a macro form? – Diaa Dec 17 '16 at 22:13
  • 1
    @DiaaAbidou I changed the file name in order not to possibly clobber already existing files as I always do with filecontents. Just a personal precaution, not mandatory for you. – egreg Dec 17 '16 at 22:21
  • Thanks for your clarification. Regarding your answer, if I want to generalize the comment delimiters (e.g. using #...# or <<...>>), is it easy to hack your second code of l3regex? – Diaa Dec 17 '16 at 22:51
  • @DiaaAbidou Well, # is always problematic in TeX; for <<...>> just use \s*<<.*>>\s*\Z – egreg Dec 17 '16 at 22:55
  • I found the same output for both \s*<<.*>>\Z and \s*<<.*>>\s*\Z, could you please tell me the meaning of \s* and the reason why there was no effect when removed? – Diaa Dec 17 '16 at 23:02
  • 1
    @DiaaAbidou That removes also trailing spaces (I don't really know whether datatool does to begin with). – egreg Dec 17 '16 at 23:05
  • I appreciate your consideration. – Diaa Dec 17 '16 at 23:08