0

I am very new to LaTex and learning it with TexMaker. I want to make a book. For this book i have 99 txt-files (each a chapter).

I want to import this chapters into my main tex-file with the command \input{}. The problem is, that the txt-files have special characters like #, @ or _ in it. When I am trying to input them, it becomes an error. I don't want to go through all the txt files and add a \ in front of the special characters. Is it possible to disable the function of these characters?

thx for your help

edit:

thank you for your comments. I try to explain it more clearly. My folderstructure is as followed:

../BookProject/myTemplate_A5_Book.tex

../BookProject/Parts/Part_1.txt...Part_99.txt

In the Part_x.txt-Files there are special characters used (i generated them with an AI so i didn't wrote them myself and i don't now all the characters which are used).

My goal is to import the text of the txt-files as paragraphs with linebreaking etc.. But when I use \input{} the first files are working and then by part 11 it end in an error (See image).

enter image description here

i hope this helps for understanding my problem.

  • I just tried \input{#@_} and it worked fine. What is the exact name of your file and which error do you get? – Ulrike Fischer Jan 13 '22 at 08:54
  • @ is not really special but # and _ and you will have to escape those there is not really any way around this as changing the meaning of # or _ breaks a lot of stuff. – daleif Jan 13 '22 at 08:54
  • @daleif sorry but lots of my files have an underscore in the name. That shouldn't be a problem. – Ulrike Fischer Jan 13 '22 at 08:55
  • @UlrikeFischer I think the OP means that the txt‘s contains special characters in the content, not in the file name. – TobiBS Jan 13 '22 at 09:19
  • @UlrikeFischer file names are not how I understood the OPs question. – daleif Jan 13 '22 at 09:22
  • @daleif ah right. But if there are simple text files without tex commands, then switching the catcodes before the input should work fine. – Ulrike Fischer Jan 13 '22 at 09:24
  • 1
    your question is not very clear. Are your files program sources or other texts with # that you need to typeset verbatim, if so you can use \verbatiminput from the verbatim package (or other fancier forms such as listings or minted) or are your files normal text that you want to typeset as paragraphs but thetext contains # and _ inline (in which case you can locally make these normal characters) – David Carlisle Jan 13 '22 at 09:32
  • your edit did not answer the question do you want to show the text files verbatim or do you want to set them as paragraphs of text with linebreaking etc. – David Carlisle Jan 13 '22 at 10:48
  • but start by using \verbatiminput{Part_1.txt} using the verbatim package (\usepackage{verbatim}) – David Carlisle Jan 13 '22 at 10:50
  • i tried \verbatiminput{Part_1.txt}and it works. But that is not what I want. I want to set them as paragraphs of text with linebreaking. – Simon Flückiger Jan 13 '22 at 10:54

2 Answers2

1

If your file is like file1.txt

#!/bin/sh

define FOO

FOO=abc

show FOO

echo $FOO

then you could input it verbatim:

enter image description here

Using

\documentclass{article}

\usepackage{verbatim}

\begin{document}

\section{File 1} \verbatiminput{file1.txt}

\end{document}

But you may prefer a fancier formatting that knows something about the format (a unix shell here)

enter image description here

\documentclass{article}

\usepackage{listings}

\begin{document}

\section{File 1} \lstinputlisting[language=sh]{file1.txt}

\end{document}

If however your text file is normal text but has LaTeX special characters like file2.txt

This is some text that costs $0.00 which is an increase of 0% on the old price
You can find it in my home directory ~/file2.txt  and ping me on
stackexchange using @

This is a second paragraph of useless text. This is a second paragraph of useless text. This is a second paragraph of useless text. This is a second paragraph of useless text. This is a second paragraph of useless text. This is a second paragraph of useless text. This is a second paragraph of useless text. This is a second paragraph of useless text. This is a second paragraph of useless text.

And here is another mention of a price in $: $30.

Then you do not want verbatim monospace rendering with all line endings preserved, but rather, you just need to set the text but treating $ as a normal character so:

enter image description here

\documentclass{article}

\usepackage[T1]{fontenc} \begin{document}

\section{File 2} { \catcode\$=12 \catcode_=12 \catcode\^=12 \catcode%=12 \catcode`~=12 \input{file2.txt} }

\end{document}

David Carlisle
  • 757,742
  • Thank you very much. This was exactly what i was looking for. Setting the \catcodeto 12 has done the job! Thank you for your help and and explaining so good! – Simon Flückiger Jan 13 '22 at 12:40
0

If I understood the problem:

  1. pandoc -o yourfile.tex yourfile.txt

  2. Then use \input{yourfile} without the .txt extension.

This will escape special characters (e.g.: # will be changed to \#) if needed (i.e., @ will not be changed at all, because it is a "normal" character except in macro names (you should not use @ in macro names, as it is reserved for internal commands (see What do \makeatletter and \makeatother do? if you ever really need to deal with internal commands of the kind of \@foo in your document).

Note that this not solve the problem with "more special UTF8 characters", that have not any special meaning in LaTeX, but just not set up for use with pdflatex. For example, ♀ (U+2640) cannot be managed "as is" by pdflatex (fatal error =no PDF, but you can load the wasysym package and use \female, for instance) whereas it can be used with xelatex or lualatex without producing a fatal error, but you still require a capable font providing this glyph (e.g., with the default font, ♀ "as is" will produce only an empty space, but just loading libertine package you will see the ♀ symbol also in the PDF.)

Fran
  • 80,769