2

I am trying to input a text file to latex. However, this text file contains special symbols like % and & which causes issues. I do not want to import the file as verbatim, it should be imported like regular text with paragraph formatting and word wrap.

6

Minimal approach:

\documentclass[]{article}

\newcommand\inputtextfile{}
\newcommand\redefinecharacter{}
\protected\def\redefinecharacter#1#2%
  {%
    \catcode`#1=13
    \begingroup
    \lccode`\~=`#1
    \lowercase{\endgroup\def~{#2}}%
  }
\protected\def\inputtextfile#1%
  {%
    \begingroup
    \redefinecharacter{\\}{\textbackslash}%
    \redefinecharacter{\{}{\{}%
    \redefinecharacter{\}}{\}}%
    \redefinecharacter{\$}{\$}%
    \redefinecharacter{\&}{\&}%
    \redefinecharacter{\#}{\#}%
    \redefinecharacter{\^}{\^{}}%
    \redefinecharacter{\_}{\_}%
    \redefinecharacter{\%}{\%}%
    \redefinecharacter{\~}{\textasciitilde}%
    \input{#1}%
    \endgroup
  }

\begin{document}
The output of our file doesn't look too good, because we input something that
contains code as text :)

\inputtextfile{\jobname}
\end{document}

Ugly output:

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.