I want to read some lines from a text file, wrap a "style" which in this case is \texttt around each line, and as such concatenate it to a macro that I can use in the document. I came up with this MWE:
\documentclass{article}
\usepackage{filecontents} % tlmgr install filecontents
\begin{filecontents*}{test-2.txt}
some more
words in
here so as
to eventually
print
\end{filecontents*}
\usepackage{xstring}
\def\totcontent{}
\def\mystyle#1{\texttt{#1}}
% related: http://tex.stackexchange.com/questions/116078/expand-command-read-from-file
\newread\myread
\makeatletter
\immediate\openin\myread=test-2.txt
\@whilesw\unless\ifeof\myread\fi{%
\readline\myread t\expandafter o\csname tmpline\endcsname %
\typeout{Got tmpline: \tmpline} %
\StrGobbleRight{\tmpline}{1}[\tmpline] % remove the ^^M newline (last 1 char)
\ifx\tmpline\empty\else %
%\edef\totcontent{\totcontent, \tmpline} % works without mystyle
%\edef\totcontent{\totcontent, \mystyle{\tmpline}} % fail, breaks
% via http://tex.stackexchange.com/a/74709/2595:
\g@addto@macro\totcontent{, \mystyle{\tmpline}} % compiles, but no content in output
\fi %
}
\immediate\closein\myread
\makeatother
\begin{document}
\typeout{totcontent is: \totcontent.}
I got this: \totcontent.
\end{document}
In all cases, I get this for the file reading:
Got tmpline: some more^^M
Got tmpline: words in^^M
Got tmpline: here so as^^M
Got tmpline: to eventually^^M
Got tmpline: print^^M
Got tmpline: ^^M
... which means the line ending is kept, so I remove the character at the end of the "string" with xstring's \StrGobbleRight.
The first case, \edef\totcontent{\totcontent, \tmpline} works fine, but has no style - the typeout then says:
totcontent is: , some more, words in, here so as, to eventually, print.
The second case, \edef\totcontent{\totcontent, \mystyle{\tmpline}}, breaks completely:
! Argument of \@xs@StrGobbleRight has an extra }.
<inserted text>
\par
l.30 }
Here I intended on using \mystyle as a placeholder macro - expecting that in concatenation \edefs it would expand down to \texttt tokens, which I remember are \protected, and so won't expand further. Obviously, that didn't work.
The third case, \g@addto@macro\totcontent{, \mystyle{\tmpline}} actually does compile, but gives:
totcontent is: , \texttt {}, \texttt {}, \texttt {}, \texttt {}, \texttt {}.
... which is close, but still not what I wanted (I wanted the \texttt to be populated by the respective arguments from the loop).
So, how can I perform the concatenation, so in the end I end up with:
totcontent is: , \texttt {some more}, \texttt {words in}, \texttt {here so as}, \texttt {to eventually}, \texttt {print}.
... such that it would also typeset as we'd expect it to?
As per comments, example with `\read`:
\documentclass{article}
\usepackage{filecontents} % tlmgr install filecontents
\begin{filecontents*}{test-2.txt}
some more
words in
here so as
to eventually
print
\end{filecontents*}
\def\totcontent{}
\def\mystyle#1{\texttt{#1}}
\newread\myread
\makeatletter
\immediate\openin\myread=test-2.txt
\@whilesw\unless\ifeof\myread\fi{%
\read\myread to \tmpline %
\typeout{Got tmpline: \tmpline} %
% \ifx\tmpline\empty\else %
\ifx\tmpline\par\else %
%\edef\totcontent{\totcontent, \tmpline} % works without mystyle
%\edef\totcontent{\totcontent, \mystyle{\tmpline}} % fail, breaks
\g@addto@macro\totcontent{, \mystyle{\tmpline}} % compiles, but no content in output
\fi %
}
\immediate\closein\myread
\makeatother
\begin{document}
\typeout{totcontent is: \totcontent.}
I got this: \totcontent.
\end{document}
Prints:
Got tmpline: some more
Got tmpline: words in
Got tmpline: here so as
Got tmpline: to eventually
Got tmpline: print
Got tmpline: \par
Then \edef\totcontent{\totcontent, \mystyle{\tmpline}} fails w:
! Use of \@pr@videpackage doesn't match its definition.
and \g@addto@macro\totcontent{, \mystyle{\tmpline}} produces:
totcontent is: , \texttt {\par }, \texttt {\par }, \texttt {\par }, \texttt {\par }, \texttt {\par }, \texttt {\par }.
... which is still not what I wanted...
\read\myread to \tmplinewill yield theStrGobble... stuff unnecessary – Oct 14 '14 at 12:25\readlineis the wrong tool, use\read. – egreg Oct 14 '14 at 12:26\readreads the whole line without LF (but the empty line at end is shown as\par), and does make StrGobble redundant. Still, that doesn't solve my problem: the\edefconcat with\mystyle{\tmpline}then fails with! Use of \@pr@videpackage doesn't match its definition.; and the\g@addto@macroproduces\texttt {\par }, \texttt {\par }, .... – sdaau Oct 14 '14 at 12:37\textttis not expandable, as far as I know – Oct 14 '14 at 12:45\edefbecause I want to expand - I used it because it was the only method I know of concatenation. What I want to achieve is to get a command as if I verbatim wrote e.g.\def\tst{\texttt{something here}, \texttt{and else}}, and then use\tstin the document to be typeset - except that here it should be the result of concatenation of lines of file input as shown in MWE. Cheers! – sdaau Oct 14 '14 at 12:50