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 4 '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 4 '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 4 '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 4 '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 4 '14 at 11:45
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.

|improve this answer|||||
  • thanks David - I'll check this out. Does the \ifx\relax$myo_test\relax basically move on if it is empty/blank? – scottyjock Feb 4 '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 4 '14 at 13:35
  • Works great! many thanks, will use this solution more often now i think – scottyjock Feb 4 '14 at 14:43

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.