I've four probable sentences in CSV file. Ex: sentence 1 here , one more sentence, one more sentence added, last sentence.

In Latex; I want to print 1 if "sentence 1 here" is present. Something like DECODE in SQL. Can you please point me URL where I can learn similar functionality.


closed as unclear what you're asking by egreg, user13907, LaRiFaRi, user31729, Joseph Wright Oct 21 '14 at 21:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This question is somewhat unclear – user31729 Sep 21 '14 at 16:12
  • I'm sorry, my English is not good. I'm creating tex file dynamically in PHP based on external inputs from CSV file. one of the value in CSV file is 'string of words'; i want to write corresponding number in tex file generated. – Pawan Mude Sep 21 '14 at 16:16
  • 5
    Could you post a minimal working example (MWE) that illustrates what you'd like to do? Right now, it isn't clear what the connection between the CSV file and the TeX file currently is or is supposed to be. Make a minimal CSV which illustrates the kind the PHP code produces and then a minimal TeX file which shows how you are using the CSV already and what you would like to do with the string. Telling me that you want something like something in SQL is not helping, I'm afraid. – cfr Sep 21 '14 at 16:23

Since you are specifically looking for SQL type functionality I suggest you explore the datatool package. The MWE reads the CSV file MyData.csv and then searches for "one more sentence" and "three more sentences":

enter image description here


  • The filecontents package was used to set up a file to read for this test case. It is not needed in you actual use case.
  • I have used newtoggle from the etoolbox package as I prefer that syntax versus the \newif syntax. But if you don't want to include an additional package it should be pretty straightforward to adapt this to use \newif or some other conditional methods.
  • Change the "Found it" text to Be just "1" (as per the question) if that is all the functionality that you desire, or adjust the code there to do something else.



%\usepackage{filecontents}% <-- Commented out to prevent overwriting MyData.csv
    sentence 1 here, 
    one more sentence, 
    one more sentence added, 
    last sentence,

    % #1 = database
    % #2 = string to chek
    % #3 = code to execute if string is found
    % #4 = code to execute if string is NOT found
             %% Found string -- we are done
             \dtlbreak% No point in searching rest of file
             % Still haven't found what we are looking for :-(


\CheckIfInDB{myDB}{one more sentence}{Found it}{Not Found!}

\CheckIfInDB{myDB}{three more sentences}{Found it}{Not Found!}


If I understand correctly you want to check a row in a CSV file and assign a number based on this sentence.

You can simply achieve this using R and sweave files (they are basically LaTeX files which have R language functionality. Remember to add knitr package to R studio.

So you begin the file as normal latex, then after \begin{document} You write the following chunk

sapply(Data, function(x){
if(x=="sentence 1 here"){
} elseif(x=="sentence2"){

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