0

I know this is a weird and stupid question, but I really need to achieve this.
I have a file containing some characters like "#", "_". For example, the file is:

abc_def#gh

And I need to input (or display the file in to the pdf) to latex without using external packages like verbatim. Achieve this just using build in latex.

I need some kind of function to replace the special character or display it in \verb style without any error.

Some limitations

  • I Can't Run Bash script inside latex as well. Because of -no-shell-escape.
  • Can't Use external packages by \usepackage command
6
  • 4
    under what circumstances could you input a file but not use a package? (as these are essentially the same thing) Oct 26, 2022 at 16:18
  • Here is how to do it with packages Include data from a .txt verbatim. Without packages or scripts it will become unnecessarily complicated.
    – dexteritas
    Oct 26, 2022 at 16:27
  • Do you mean you want to typeset a file name or do you want to include the file contents of a file with that name (so \input{foo_bar#baz})?
    – Skillmon
    Oct 26, 2022 at 16:30
  • Why do you use the tags verbatim and shell-escape, if you don't want to use them? I removed them.
    – dexteritas
    Oct 26, 2022 at 16:30
  • What about \csname usepackage\endcsname{verbatim} or \input{verbatim.sty}?
    – user202729
    Oct 26, 2022 at 17:46

4 Answers 4

5

If you don't want to use LaTeX packages then the following solution needn't LaTeX at all. Only TeX primitives and Plain TeX basic macros are used here. But the solution works in LaTeX too, because the mentioned Plain TeX macros \dospecials, \obeylines and \obeyspaces are implemented in LaTeX kernel too.

\def\inputverbatim #1{\bgroup
  \def\do##1{\catcode`##1=12 }\dospecials
  \def\par{\endgraf\noindent\null}\obeylines\obeyspaces
  \tt \noindent
  \input{#1}
  \egroup
}

%%% test:
\inputverbatim {file.txt}
7
  • There's much more than \dospecials, \obeyspaces and \obeylines to get a working verbatim framework. Breaking ligatures, just as an example.
    – egreg
    Oct 27, 2022 at 7:21
  • ligatures in \tt font?
    – wipet
    Oct 27, 2022 at 10:29
  • I'm sure you are able to read the output of tftopl cmtt10 to see (LIGTABLE (LABEL O 41) (LIG O 140 O 16) (STOP) (LABEL O 77) (LIG O 140 O 17) (STOP) ) The situation is even worse when using T1 encoded fonts.
    – egreg
    Oct 27, 2022 at 11:40
  • Now, 2022, somebody are using such archaic fonts?
    – wipet
    Oct 27, 2022 at 11:49
  • OpenType fonts usually have much more ligatures than those two.
    – egreg
    Oct 27, 2022 at 12:34
3

enter image description here

It is hard to see why you could not use \verbatiminput

but if myfile2 has

line 1
   abc_def#gh
line 3
   $$$$$$
line 5

then

\documentclass{article}

\makeatletter
\edef\qqq#1{%
\noexpand\begin{verbatim}
\noexpand\input{#1}%
\@backslashchar end\string{verbatim\string}}
\makeatother

\begin{document}


\qqq{myfile2}

\end{document}

However if verbatim.sty is not available to be used,article.cls should not be available either, both are part of the core latex release. So I do not see how any latex document could work.

0

I suggest listingsutf8 package and use it like following:

\lstinputlisting[inputencoding=utf8/cp1250,frame=hnone,numbers=none,extendedchars=true]{source.txt}

The second option is to use \detokenize command like

\detokenize{abc_def\#gh}

But as you see, # must be quoted with backslash.

4
  • listings package is Ok but doesn't answer the question which (for some strange reason) asks for no packages to be used Oct 26, 2022 at 17:56
  • It would be painful to achieve this without external package. At least the author can provide package files in the same directory as LaTeX source. Oct 26, 2022 at 17:57
  • but you posted this after another answer which did as requested in 4 lines of code, so not that painful. Oct 26, 2022 at 18:12
  • \detokenize appends space-tokens behind control-word-tokens, e.g., \detokenize{\relax\relax} yields \relax␣\relax␣. \detokenize doubles hashes/explicit character-tokens of category 6(parameter). With \detokenize the result depends on the value of the integer-parameter \escapechar. How to combine \detokenize with \input? Oct 26, 2022 at 22:03
0

This is how I might go at it:

%-----------------------------------------------------------------
% Let's create an external text file  SomeFile.tex  which is to be 
% input verbatim:
%-----------------------------------------------------------------
\begin{filecontents*}{SomeFile.tex}
Line 1: abc_def#gh
Line 2: $$$_def#gh
Line 3: %%%_def#gh

\end{verbatim}

Line 7: bla~~~bla
\end{filecontents*}
%-----------------------------------------------------------------


\documentclass{article}

\newcommand\Exchange[2]{#2#1}

\begin{document}

Text

\Exchange{\input{SomeFile.tex}}{\begin{verbatim}}\end{verbatim}

Text

\end{document}

enter image description here



If you don't wish things to be typeset in typewriter-mode and just wish TeX to treat special characters like ordinary characters that shall occur within the text while typesetting as usual, you can try s.th. like this:

%-----------------------------------------------------------------
% Let's create an external text file  SomeFile.tex  which is to be 
% input:
%-----------------------------------------------------------------
\begin{filecontents*}{SomeFile.tex}
Line 1: $&#^_~%\{}
Line 2: $&#^_~%\{}
\end{filecontents*}
%-----------------------------------------------------------------

\documentclass{article}

\makeatletter
\@ifdefinable\secondwhentypesetting{\protected\def\secondwhentypesetting#1#2{#2}}
\newcommand\unexpandedwhennottypesetting[1]{\noexpand\unexpandedwhennottypesetting{\unexpanded{#1}}}
\DeclareRobustCommand\myactivateasnospecial{\@dblarg\@myactivateasnospecial}%
\newcommand\@myactivateasnospecial[2][]{%
  \begingroup\lccode`\~=`#2\relax
  \lowercase{\endgroup\def~}{\secondwhentypesetting\unexpandedwhennottypesetting{#1}}%
  \catcode`#2=13\relax
}%
\makeatother

\DeclareRobustCommand\TypesetFileWithSpecialsAsOrdinaryCharacters[1]{%
  \begingroup
  % switch special characters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \let\do=\myactivateasnospecial
  \do\$%
  \do\&%
  \do\#%
  \do[\ifmmode\hat{}\else\textasciicircum\fi]\^%
  \do[\ifmmode\_\else\textunderscore\fi]\_%
  \do[\ifmmode\sim\else\textasciitilde\fi]\~%
  \do\%%
  \do[\csname\ifmmode\else text\fi backslash\endcsname]\\%
  \do\{%
  \do\}%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \input{#1}%
  \endgroup
}%

\begin{document}

\TypesetFileWithSpecialsAsOrdinaryCharacters{SomeFile.tex}

$$\TypesetFileWithSpecialsAsOrdinaryCharacters{SomeFile.tex}$$

\end{document}

enter image description here

You must log in to answer this question.

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