4

I've set up a custom environment with DefineVerbatimEnvironment. Within that environment I'd like to \input{} files, which works all fine and well until one of them includes a hyphen. Here's an example that demonstrates the problem:

\documentclass{article}

\usepackage{fancyvrb}

\DefineVerbatimEnvironment{terminal}{Verbatim}{commandchars=\\\{\}}

\begin{document}

\begin{terminal}
  \input{some-file}
\end{terminal}

\end{document}

If I try to \input{file} the contents of the file show up in the verbatim environment just as I'd expect. But if I try and \input{some-file} I get the following error:

! LaTeX Error: File `more\unhbox \voidb@x \kern \z@ \char `\discretionary {-}{}{}input.tex' not found.

Obviously, my hyphen is being translated into \unhbox ... {-}{}{} - how can I stop this happening?

  • Welcome to TeX.SX. A tip: If you indent lines by 4 spaces, then they're marked as a code sample. You can also highlight the code and click the "code" button ({}). – Claudio Fiandrino Jan 24 '13 at 16:43
  • You can't do like this. You need a "verbatim input": \VerbatimInput{some-file} – egreg Jan 24 '13 at 16:45
  • egreg - if I do this, I get "! LaTeX Error: Something's wrong--perhaps a missing \item.". This happens whether or not the file name contains a hyphen or not.

    I am just replacing \input{xxx} in my example with \VerbatimInput{xxx} - is this what you mean?

    – John Graham Jan 24 '13 at 17:02
  • @JohnGraham No, you don't need the terminal environment at all. – egreg Jan 24 '13 at 19:26
  • Do you need to interpret TeX commands int the input file? – egreg Jan 24 '13 at 20:57

2 Answers2

3

You just need to move the - somewhere safe, for example:

\documentclass{article}

\usepackage{fancyvrb}

\DefineVerbatimEnvironment{terminal}{Verbatim}{commandchars=\\\{\}}
\newcommand\somefile{\input{some-file}}
\begin{document}

\begin{terminal}
  \somefile
\end{terminal}

\end{document}
David Carlisle
  • 757,742
  • Thanks David - I went for a slightly more flexible form of the above, adding a new command: \newcommand{\hyphenate}[2]{#1-#2} used like so in my terminal environment: \input{\hyphenate{some}{file}}. – John Graham Jan 24 '13 at 19:58
1

It mostly depends on what you want to input, but either if you need to interpret TeX commands in the input file or not, \VerbatimInput seems the way to go:

\begin{filecontents*}{v-inp.sh}
#!/bin/env bash
echo "${PATH}"
exit
\end{filecontents*}
\begin{filecontents*}{v-inp.tex}
\textcolor{red}{abc}def
\end{filecontents*}

\documentclass{article}
\usepackage{color}
\usepackage{fancyvrb}
\begin{document}

\VerbatimInput{v-inp.sh}

\bigskip

\VerbatimInput[commandchars=\\\{\}]{v-inp.tex}

\end{document}

Note that your construction

\begin{terminal}
\input{\hyphenate{v}{inp.sh}
\end{terminal}

would not give the expected result break. On the other hand, if you want to input a TeX file, treating it as "almost verbatim", you just need the alltt package:

\documentclass{article}
\usepackage{color}
\usepackage{alltt}

\begin{document}

\begin{alltt}
\expandafter\input\expandafter{\detokenize{v-inp.tex}}
\end{alltt}

will work as well. You can define a better command:

\newcommand{\vinput}[1]{%
  \expandafter\input\expandafter{\detokenize{#1}}%
}
egreg
  • 1,121,712