When using the fancyvrb
environment Verbatim
, you can gobble the first n
characters of each line before printing it out. I'd like to be able to do something similar, but not via verbatim.
In the following MWE, I would like to read in the file:
\textbf{This is the first line.}
%% \textbf{This is the second line.}
and have LaTeX process it as though it'd been passed a file:
\textbf{This is the first line.}
\textbf{This is the second line.}
Here's an example of what I've tried, but it doesn't work as hoped:
\begin{filecontents*}{aetesting}
\textbf{This is the first line.}
%% \textbf{This is the second line.}
\end{filecontents*}
\documentclass{article}
\usepackage{catchfile}
\def\gobblefirst#1#2#3#4{}
\def\mysetup{\let\do\gobblefirst\dospecials}
\CatchFileDef{\aemyfile}{aetesting}\mysetup
\begin{document}
\aemyfile
\end{document}
UPDATE
I can almost get this to work (without catchfile
)
\begin{filecontents*}{aetesting}
The character of the First Caesar has perhaps never been worse
appreciated than by him who in one sense described it best; that is,
\textbf{This is the first line.}\par
%% \textbf{This is the second line.}
%% Great as Caesar was by the benefit of his original nature, there can be
no doubt that he, like others, owed something to circumstances; and
\end{filecontents*}
\documentclass{article}
\makeatletter
\newread\file
\newcommand\aeomit[2]{}
\newcommand\myaccumulatedfile{}
\newcommand*\aegobbleinput[1]{%
\IfFileExists{#1}{%
\openin\file #1
\begingroup
\endlinechar\newlinechar
\loop\unless\ifeof\file
\obeyspaces
\readline\file to\foo
\edef\myaccumulatedfile{\myaccumulatedfile\expandafter\@gobbletwo\foo\relax\relax}%%
\repeat
\closein\file
\expandafter\endgroup
\scantokens\expandafter{\myaccumulatedfile}
}{%
\errmessage{File `#1' doesn't exist!}%
}%
}
\makeatother
\begin{document}
Try:
\aegobbleinput{aetesting}
\end{document}
The problem is that leading spaces are ignored and I don't want them ignored.
Also my line break before \textbf{This is the first line.}
is no longer properly treated as a paragraph break. :(
Further update
Thinking I might try this in LaTeX3, I run into even more troubles.
\usepackage{xparse}
\ExplSyntaxOn
\ior_new:N \g_ae_fh_ior
\tl_new:N \l_ae_fh_line_tl
\cs_new:Npn \__ae_gobble:n #1
{
\file_if_exist:nTF { \__ae_open:n { #1 } }
{ \typeout{no file!} }
}
\cs_new:Npn \__ae_open:n #1
{
\ior_open:Nn \g_ae_fh_ior { #1 }
\ior_str_map_inline:Nn \g_ae_fh_ior { \tl_gput_right:Nn \l_ae_fh_line_tl {##1 \par}}
\ior_close:N \g_ae_fh_ior
}
\NewDocumentCommand{\aegobbleinput}{m}
{
\__ae_gobble:n {#1}
}
\ExplSyntaxOff
Here I'm not even trying to truncate anything. But I can't get LaTeX to read the file. I get one of two errors with this code. When I try to use the inline
map, I get the error
! Illegal parameter number in definition of \l__file_internal_name_tl.
<to be read again>
1
l.6 \aegobbleinput{aetesting.tex}
?
If I get rid of that line, even though getting rid of it should result in nothing getting parsed, I get the error message
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! LaTeX error: "kernel/space-in-file-name"
!
! Space in file name '\ior_open:Nn \g_ae_fh_ior {aetesting.tex}\ior_close:N
! \g_ae_fh_ior '.
!
! See the LaTeX3 documentation for further information.
!
! For immediate help type H <return>.
!...............................................
l.6 \aegobbleinput{aetesting.tex}
?
which makes absolutely no sense to me particularly because I'm very closely following the example as posted here.
expl3
definition, you are missing the file name argument for\file_if_exist:nTF
, hence the error.\let\do\gobblefirst\dospecials
is strange as the meaning of\dospecials
ismacro:->\do \ \do \\\do \{\do \}\do \$\do \&\do \#\do \^\do \_\do \%\do \~
. But I am not familiar with\CatchFileDef
so perhaps something else happens.eTeX
documentation there are only 2 or 3 lines of explanation about\readline
. I really feel in the dark.end of line
act to suppress the first characters of the next line.TeX by Topic
has some explanations about either reading line by line or inputting in one go a file. AndeTeX
has added stuff such as\everyeof
.\readline
seems to be for some sort of verbatim input, I am not familiar with its use, having only some limited experience withscantokens
for example.