I make use of a package (gnuplottex) that writes contents of an environment to a file, using the \verbatim@start/processline/end commands (by \immediate\write\FileHandle{\the\verbatim@line}). Now I want to modify the package to insert a couple of user-defined lines at the start of every file.

I defined a token list to store the lines and two commands to set these header lines:


The tokens I then write to the file before the \verbatim@start, with \immediate\write\FileHandle{\the\headerlines}. This works fine for a single line, but I need to write several lines to the file, so preserve newlines in the token and write them to the files. Right now, this does not happen: everything gets put on a single line. How do I preserve newlines in the token list and write as separate lines to the file?

In other words:

\setheaderlines{header line 1
header line 2}

gets written to the file as

header line 1 header line 2`

instead of

header line 1
header line 2
  • 1
    \setheaderlines{Line 1^^JLine2} should do
    – egreg
    Aug 5 '14 at 10:14
  • Indeed, then the ^^J character is written as a line break to the file. However, I'd rather be able to \setheaderlines{Line 1 Line 2}. Isn't that somehow possible, too?
    – Geert F
    Aug 5 '14 at 11:23
  • ^^ there's obviously supposed to be a line break between Line 1 and Line 2 there. Linebreaks.. ugh! ;)
    – Geert F
    Aug 5 '14 at 11:54
  • 1
    @egreg This question is not explicitly duplicated. The token list from \toks register is written here, no macro body. It means that you cannot set ^^M as active and to count on the expansion during \ẅrite. This expansion isn't done in toks register. Please, explain this to the other users they marked this as duplicate. The name "token list" is included in the title of this thread.
    – wipet
    Aug 5 '14 at 14:32

You can set the ^^M to the normal character when \headerlines are scanned. It means that token list includes ^^M characters. Finally, you have to set this character as \newlinechar when this token list is printed.



   \lowercase{\egroup\headerlines\expandafter{\the\headerlines X}}%


line 1
line 2
line 3
line 4


Edit: I've added the \addtoheaderlines macro and replaced the \setheaderlines macro to the special usage of \addtoheaderlines.

  • That does the trick. :) However, I'm still struggling with my \addtoheaderlines. Could you help me out there, too?
    – Geert F
    Aug 5 '14 at 12:26
  • OK, I've upgraded my answer.
    – wipet
    Aug 5 '14 at 12:45
  • Cool, thanks. :) I don't understand why the \egroup is in its place, but it works. Does this group delimit the catcode change; and if so: why is it indeed before the \headerlines is set? PS I've included a ^^J right between \the\headerlines and #1 so that a single line \addtoheaderlines{line} is added on its own line.
    – Geert F
    Aug 5 '14 at 12:54
  • The processing is done in the following steps: \bgroup, catcode setting, scanning parameter, \egroup, \headerlines setting. The catcode have to be set when parameter is scanned.
    – wipet
    Aug 5 '14 at 13:05
  • ad. PS. I don't understand your ^^J handling. My code uses ^^M instead ^^J as \newlinechar. There cannot be two \newlinechars at one time.
    – wipet
    Aug 5 '14 at 13:09

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.