8

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.

2
  • 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
    Commented Jun 2, 2017 at 17:22
  • Could you please provide a short usage example? I'm not sure what a lexer is, let alone a text lexer... Commented Jun 2, 2017 at 20:37

2 Answers 2

9

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

2
  • 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! Commented Jun 3, 2017 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
    Commented Jun 3, 2017 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .