1

I have a script written in perl which harvest data from an sql database, then produces a printed report in latex.

I was looking to put an IF loop in the latex part of the script to print one set of text if the data existed in the sql database, and a different set of text if it does not.

Looks like etoolbox is promising here, but can it take a variable (which I can set to be anything - 1,0,T,F etc)?

I found this example on the site which shows how to set up the loop but without a variable (LaTeX conditional expression).

\newtoggle{paper}

which is set with either

\toggletrue{paper}
\togglefalse{paper}

And to use it:

\iftoggle{paper}{%
  % using paper
}{%
  % electronic
}

EDIT: What I am wondering is if I can use a variable from earlier in my script (e.g. $myo_text) in the eftoolbox toggle.

e.g.

\newtoggle{$myo_text}

Or is there another way of doing this? Is it even possible to carry a variable into the latex part of the script from the sql query and use it in a conditional loop?

EDIT 2: Here is a trimmed latex chunk from my script. Just now it prints out these perl variables just fine ($myo_test, $myo_reporter1 etc).

print FH <<END;

\\textbf{EXERCISE TOLERANCE TEST} \\newline
\\begin{small}
\\textbf{Stress Test:} $myo_test \\newline
\\textbf{Rest ECG:} $myx_rest_ecg \\newline
\\textbf{Stress ECG:} $myx_stress_ecg \\newline  
\\textbf{Performed by:} $myx_performed1 \\newline
\\end{small}

\\textbf{Reported By:} $myo_reporter1 \\hspace{2em} \\textbf{Signature:}

\\end{document}


END

I would like to put a loop in this latex chunk that does not print this part of the report if $myo_text is blank/empty.

  • 1
    This has a variable, precisely the boolean paper. – egreg Feb 04 '14 at 10:01
  • Hi! I'm not sure it's clear what you are after. Could you please show what exactly you try to achieve? Yeah, and Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format! ;) – yo' Feb 04 '14 at 10:11
  • It sounds to me that you are more after \ifstrequal{\myo_text}{true}{True case}{False case} or some of the other test commands. – Andrew Swann Feb 04 '14 at 10:35
  • the perl is writing a text file which is then processed by latex. Your question seems to be entirely about perl (so not really on topic here) you can obviously write your perl so that the value of the variable $myo_text rather than the string $myo_text gets written to the text file to be processed by latex. How you do that depends on the structure of your perl, something like print "\\newtoggle{" . $myo_text . "}" – David Carlisle Feb 04 '14 at 11:40
  • Hi David - the perl part is fine and extracts all data I need, just need a bit of help with the latex section of the script to print it. I'll add some more code to the question which may help. I am not an expert in latex so need a nudge when it comes to this - the big question is how do I use my variable $myo_text, which is generated in the perl section, in the later latex section of the script ($myo_text is either blank or contains text). – scottyjock Feb 04 '14 at 11:45
  • @scottyjock as I say latex cares nothing about the perl, you just need to get perl to write the correct latex (whatever that is) the latex part is the same whether the latex is constructed by hand or a perl script – David Carlisle Feb 04 '14 at 11:52
  • Hi David - I've added a trimmed chunk of latex to my question, I'm constructing the latex inside the perl script as shown, and managing to feed the perl variables into it directly for printing. Just need some logic (i have none..:)) to test if $myo_test is blank/enmpty. Many thanks for your comments – scottyjock Feb 04 '14 at 11:57
  • test it in the perl and then just write out the two different latex strings you want in the empty or non empty case. there is no point in writing $myo_test into the latex file although if you just want a simple if test you could do it in the latex, I'll add something – David Carlisle Feb 04 '14 at 11:59
  • I see what you mean, but $myo_test is really a paragraph of text. I still would like a test/loop in latex, which is what i am struggling to do or understand. I want $myo_test and all the other $myo variables shown to be written in IF $myo_test is not empty. Only way I can think of is to test if empty in perl then do 2 distinct latex chunks which are called in each case - is that what you are saying? Must be a more elegant way to do an IF test in latex? – scottyjock Feb 04 '14 at 12:06
  • Why don't you just use placeholders in a LaTeX template document, and then fill this in via LaTeX. For example in the template XYZ_unique_name_XYZ, and then have you perl script replace all placeholders, and remove the empty ones. – daleif Feb 04 '14 at 12:16

1 Answers1

2

LaTeX has no knowledge of the perl variables, you just need to write the correct latex. In general it's probably best to test in the perl and then just write appropriate latex

something like

if($myo_test !="") {

print FH <<END;

\\textbf{EXERCISE TOLERANCE TEST} \\newline
\\begin{small}
\\textbf{Stress Test:} $myo_test \\newline
\\textbf{Rest ECG:} $myx_rest_ecg \\newline
\\textbf{Stress ECG:} $myx_stress_ecg \\newline  
\\textbf{Performed by:} $myx_performed1 \\newline
\\end{small}

\\textbf{Reported By:} $myo_reporter1 \\hspace{2em} \\textbf{Signature:}

END

}

but you could use any latex test for empty eg \ifx\relax..\relax and do simple tests in the latex

print FH <<END;
\\ifx\\relax$myo_test\\relax
\\else
\\textbf{EXERCISE TOLERANCE TEST} \\newline
\\begin{small}
\\textbf{Stress Test:} $myo_test \\newline
\\textbf{Rest ECG:} $myx_rest_ecg \\newline
\\textbf{Stress ECG:} $myx_stress_ecg \\newline  
\\textbf{Performed by:} $myx_performed1 \\newline
\\end{small}
\\fi

\\textbf{Reported By:} $myo_reporter1 \\hspace{2em} \\textbf{Signature:}


\\end{document}


END

But in either style there is no sense in which latex is accessing the perl variable, perl is just writing out a static file to be processed later by latex.

David Carlisle
  • 757,742
  • thanks David - I'll check this out. Does the \ifx\relax$myo_test\relax basically move on if it is empty/blank? – scottyjock Feb 04 '14 at 12:52
  • There are lots of tests for empty, that one tests if two adjacent tokens are \relax which they will be if perl writes \ifx\relax\relax and won't be if perl writes ifx\relax foo \relax (LaTeX of course never sees the variable reference) – David Carlisle Feb 04 '14 at 13:35
  • Works great! many thanks, will use this solution more often now i think – scottyjock Feb 04 '14 at 14:43