5

is there a package that allows one to import a selection of lines from an external .txt file?

I know minted has a command that can be customized this way: \inputminted[firstline=3,lastline=5]{c}{hello.c} (see this link), but I'm looking for something more neutral, not specific to any programming language. This is because I'm writing a commands.txt file with a long list of Cisco's router/switch configuration commands that I'm learning, and I'd like to import only few required lines as a way to showcase examples. Obviously, minted doesn't include any 'cisco' language. I know of \verb and the verbatim environment, but I gather there could be problems with breaking long lines.

  • According to this answer, there is a text option for minted that just prints the plain text without highlighting. Does that fulfill your needs? – Tiuri Jun 2 '17 at 17:22
  • Could you please provide a short usage example? I'm not sure what a lexer is, let alone a text lexer... – WobblyWindows Jun 2 '17 at 20:37
5

Instead of specifying a proper computer language for syntax highlighting in the environments of the minted package, you can use the option text. This will just typeset the input verbatim-like without syntax-highlighting but with the full functionality of the minted package.

In your case, you probably want to do use \inputminted with the firstline and lastline option. Additionally, just give text as first mandatory argument:

\documentclass{article}

\usepackage{minted}
\usepackage{filecontents}

\begin{filecontents*}{\jobname.C}
int main() {
    printf("hello, world");
    return 0;
}
\end{filecontents*}

\begin{document}

\inputminted[firstline=2,lastline=3]{text}{\jobname.C}

\end{document}

enter image description here

  • Is it necessary that I use package "filecontents" in the preamble? Or is it just for the sake of the example? I could just go ahead and write \inputminted[firstline=2,lastline=3]{text}{commands.txt}, right? (No backslash preceding the file's name, correct?) -- Sorry for the dumb questions! – WobblyWindows Jun 3 '17 at 8:31
  • 1
    That's right. I followed this guidelines on how to write a Minimal Working Example. filecontents is used to have the external file within my LaTeX document, and \jobname.C just dynamically assignes the name of the TeX file to the commands file. In your real-world document, you will of course won't need to use filecontents and just give the filename as-is. – Tiuri Jun 3 '17 at 8:54
2

Depending on what's the contents of the file, this might be what you want.

\begin{filecontents*}{\jobname.txt}
This is line 1 with \special{characters}
This is line 2 with \special{characters}
This is line 3 with \special{characters}
This is line 4 with \special{characters}
This is line 5 with \special{characters}
This is line 6 with \special{characters}
This is line 7 with \special{characters}
This is line 8 with \special{characters}
This is line 9 with \special{characters}
\end{filecontents*}

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\verblines}{mmm}
 {
  \begin{flushleft}\ttfamily
  \wobbly_verblines:nnn { #1 } { #2 } { #3 }
  \end{flushleft}
 }

\int_new:N \l__wobbly_verblines_index_int
\ior_new:N \g__wobbly_verblines_file_stream

\cs_new_protected:Nn \wobbly_verblines:nnn
 {
  \ior_open:Nn \g__wobbly_verblines_file_stream { #1 }
  \int_zero:N \l__wobbly_verblines_index_int
  \ior_str_map_inline:Nn \g__wobbly_verblines_file_stream
   {
    \int_incr:N \l__wobbly_verblines_index_int
    \int_compare:nT { #2 <= \l__wobbly_verblines_index_int <= #3 } { ##1 \\ }
   }
  \ior_close:N \g__wobbly_verblines_file_stream
 }
\ExplSyntaxOff

\begin{document}

Here are lines 1 to 3
\verblines{\jobname.txt}{1}{3}

Here are lines 2 to 5
\verblines{\jobname.txt}{2}{5}

Here are all the lines
\verblines{\jobname.txt}{0}{100}

\end{document}

The filecontents* environment is used just for making the example self-contained. The last example shows what happens if the arguments are beyond the number of lines in the file.

enter image description here

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.