1

I am trying to figure out how I can use HEREDOC syntax to interpret variables but ignore the backslash character. Or use NOWDOC syntax to allow for the interpretation of variables. An example of what I am trying to do:

$title = "My title here";
$date = "Aug 12, 2017";

$latex_code = <<<LCODE
    \documentclass{article}

    \usepackage{graphicx}

    \pagestyle{head}
    \firstpageheader{
        $title
        $date
     }
LCODE;

file_put_contents("article.tex", $latex_code);

I want to ignore all slashes but interpret the variables $title and $date. Is there a way to do this without exiting from a HEREDOC or NOWDOC block?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
kojow7
  • 10,308
  • 17
  • 80
  • 135
  • Can you escape it? What do you mean by "ignore" the backslash, actually? Just interpret it literally, or not include it at all? – Don't Panic Sep 15 '17 at 17:18
  • I'm not sure 2 vars inside curly brackets will be translated. Suggest wrapping vars you want translated with their own pair of curly brackets.. \firstpageheader {$title} {$date} LCODE; – Duane Lortie Sep 15 '17 at 17:21
  • @Don'tPanic I am trying to export this all to a text file exactly as is, but with variables interpreted. I have updated my question to indicate this with the file_put_contents line. – kojow7 Sep 15 '17 at 17:24
  • @DuaneLortie There seems to be no issue with the braces. – kojow7 Sep 15 '17 at 17:25
  • What output are you getting? What output are you expecting? – user176717 Sep 15 '17 at 17:57

1 Answers1

3

At the risk of misunderstanding the intended output, I think you could just escape the backslashes. (I'm assuming you want the single backslashes included in the output.)

$title = "My title here";
$date = "Aug 12, 2017";

$latex_code = <<<LCODE
    \\documentclass{article}

    \\usepackage{graphicx}

    \\pagestyle{head}
    \\firstpageheader{
        $title
        $date
     }
LCODE;

Obviously if you don't do that, some of them (e.g. \f) will be escape sequences that will be interpreted.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80