1

I get data from a csv file using the datatool package:

DTLgetvalueforkey{\thenames}{NAMES}{data}{BD}{#2}
\ifthenelse{\equal{\thenames}{\dtlnovalue}}{}{
  \specialtext{\thenames}
}

based on datatool: getting a specific value given the value of another column.

Based on Split a string and apply an operation to each segment (or "word") i try write every "word" of the result \thenames into a separate line like this:

\newcommand{\specialtext}[1]{%
    \noexpandarg    
    \StrSubstitute{#1}{;}{\newline}[\myspecialtext]\textbf{\myspecialtext}}

However my call to that function doesn't result in a multiline output but in a oneline output: \specialtext{\thenames} However my test call: \specialtext{foo;bar} works perfectly as expected.

My question is how do I need to pass \thenames to \specialtext so that it works the same as for foo;bar?

EDIT: Here is my MWE:

content of mwedata.csv:

BD, NAMES
2606,   foo bar; foo2 bar2
2706,   foo3 bar3

mwe.tex:

\documentclass[a4paper]{article}

\usepackage{ifthen}
\usepackage{datatool}
\usepackage{xstring}

\DTLloaddb{data}{mwedata.csv}

\newcommand{\specialtext}[1]{%
    \noexpandarg    
    \StrSubstitute{#1}{;}{\newline}[\myspecialtext]\textbf{\myspecialtext}
}

\newcommand*{\thenames}{}

\newcommand{\printSplittedNames}[2]{
    \DTLgetvalueforkey{\thenames}{NAMES}{data}{BD}{#1}
    \ifthenelse{\equal{\thenames}{\dtlnovalue}}{}{
      \specialtext{\thenames}
      %\thenames
      %\specialtext{foo bar; foo2 bar2}
    }
}

\begin{document}
2606:\\
\printSplittedNames{2606}\\
\newline
2706:\\
\printSplittedNames{2706}\\
\end{document}

I narrowed it down to these three lines:

\specialtext{\thenames}
%\thenames
%\specialtext{foo bar; foo2 bar2}

Using the first is what I want, but the result is printed in one line. The seconds just prints the expected one line as return from datatool. In the third the value returned by datatool is passed statically to \specialtext and works as expected, however I don't want the value to be static of course but to come from the mwedata.csv value.

2
  • An MWE would really be nice here, particularly since you didn't say how you are creating \thenames. Aug 16 '15 at 15:04
  • @JohnKormylo I provided a MWE, sorry for not providing it in the first place.
    – soriak
    Aug 16 '15 at 16:08
1

It's an expansion problem: if you add \show\myspecialtext before \textbf, you can see that the expansion is \thenames. So you want to use \expandarg, instead of \noexpandarg.

The \printSplitNames macro should only have one argument, not two.

\begin{filecontents*}{\jobname.csv}
BD, NAMES
2606,   foo bar; foo2 bar2
2706,   foo3 bar3
\end{filecontents*}

\documentclass[a4paper]{article}

\usepackage{ifthen}
\usepackage{datatool}
\usepackage{xstring}

\DTLloaddb{data}{\jobname.csv}

\newcommand{\specialtext}[1]{%
  \saveexpandmode
  \expandarg    
  \StrSubstitute{#1}{;}{\newline}[\myspecialtext]%
  \restoreexpandmode
  \textbf{\myspecialtext}%
}

\newcommand*{\thenames}{}

\newcommand{\printSplitNames}[1]{%
  \DTLgetvalueforkey{\thenames}{NAMES}{data}{BD}{#1}
  \ifthenelse{\equal{\thenames}{\dtlnovalue}}{}{\specialtext{\thenames}}%
}

\begin{document}

\noindent
2606:\\
\printSplitNames{2606}

\noindent
2706:\\
\printSplitNames{2706}

\end{document}

enter image description here

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.