4

I'm looking for a LaTeX package that enables an environment sort of like "verbatim" in that it prints each and every unicode character exactly as typed, but... 1> does NOT replicate linebreaks 2> uses the \normalfont 3> justifies, hyphenates and prints the text as LaTeX would normally

I would use it to typeset paragraphs of text from a txt file. (I want it typeset, I don't want it to look like it was printed from notepad or a typewriter)

The text is full of @ # $ % characters that otherwise require special attention to print out, so I figure a lot of folks run into this routinely. Is there a package to help?

  • Please provide specific examples of entire paragraphs. – Mico Apr 1 '16 at 5:37
  • 1
    Have you looked at the fancyvrb package? There are lots of options available there that might suit your needs. – Tony Mottaz Apr 1 '16 at 6:32
2

If you don't need commands in the text to typeset, a simplified version of jfbu's macros is available. It's also easy to turn the environment into an “input” type macro.

As jfbu's macros, this code relies on using T1 encoded fonts.

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\makeatletter
\newcommand\textverbatim{%
  \let\do\@makeother\dospecials
  \catcode` =10
  \text@verbatim
}
\edef\text@verb@end{%
  \expandafter\@firstoftwo\string\x 
  end\string{textverbatim\string}%
}
\begingroup\def\x{\endgroup\long\def\text@verbatim##1}
\expandafter\x\text@verb@end{%
  \ignorespaces#1\endgroup\ignorespaces
}

\newcommand{\textverbatiminput}[1]{%
  \begingroup
  \let\do\@makeother\dospecials
  \catcode` =10
  \input{#1}%
  \endgroup
}
\makeatother




\begin{document}

\begin{textverbatim}
I'm looking for a LaTeX package that enables an environment
sort of like "verbatim" in that it prints each and every 
unicode character exactly as typed, but... 1> does NOT 
replicate linebreaks 2> uses the \normalfont 3> justifies, 
hyphenates and prints the text as LaTeX would normally

I would use it to typeset paragraphs of text from a txt file. 
(I want it typeset, I don't want it to look like it was 
printed from notepad or a typewriter)

The text is full of @ # $ % characters that otherwise require 
special attention to print out, so I figure a lot of folks run 
into this routinely. Is there a package to help?

Here are most characters (originally copied from the pdf 

http://www.unicode.org/charts/PDF/U0100.pdf

\also\some\backslash#@&%%%

./myverbatim.tex:209: Package inputenc Error: Unicode char (U+126)

Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

Test of Unicode in verbatim:
ĀāĂ㥹 ĆćĈĉĊċČč ĎďĐđ ĒēĔĕĖėĘęĚě ĜĝĞğĠġĢģ Ĥĥ
ĨĩĪīĬĭĮįİı Ĵĵ Ķķ ĹĺĻļĽľL·l·Łł ŃńŅņŇň Ŋŋ ŌōŎŏŐőŒœ
ŔŕŖŗŘř ŚśŜŝŞşŠš ŢţŤť ŨũŪūŬŭŮůŰűŲų Ŵŵ ŶŷŸ ŹźŻżŽž
\end{textverbatim}
Back to \textbf{normal}: $E=mc^2$.

\textverbatiminput{\jobname}

\end{document}

enter image description here

  • 1
    I recommend accepting that answer rather than mine; for some reason I wanted to try out something different, which would work if the spaces were not normal, but with normal spaces, the approach in my answer has the defect explained in my comments. The approach here is better. – user4686 Apr 1 '16 at 22:10
  • that's honorable! your answer offered a lot of insights, so it comes in handy – user12711 Apr 2 '16 at 19:43
5

You don't need a package for this.

The update at bottom of answer is for slightly more complicated coding which requires \end{myverbatim} and not only \end{myver to decide that the end of the environment has been hit.

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\makeatletter
\catcode`\{ 12 \catcode`\( 1
    \def\myverbatim@endtag (end{myver}
\catcode`\{ 1 \catcode`\( 12

\def\myverbatim@backslash #1#2#3#4#5#6#7#8#9%
{%
    \def\myverbatim@temp{#1#2#3#4#5#6#7#8#9}%
    \ifx\myverbatim@temp\myverbatim@endtag
          \expandafter\myverbatim@stop
    \else \@backslashchar\expandafter\myverbatim@temp
    \fi
}

\def\myverbatim@stop batim#1{\end{myverbatim}}

\begingroup
    \lccode`\~ 92
    \lowercase{%
\gdef\myverbatim@makeintelligentbackslash 
   {\catcode92 \active \let~\myverbatim@backslash}%
              }
\endgroup   
\newenvironment{myverbatim}{\let\do\@makeother \dospecials
   \catcode32 10 % normal spaces
   \myverbatim@makeintelligentbackslash % exit properly
   }{}
\makeatother

\begin{document}

\begin{myverbatim}
I'm looking for a LaTeX package that enables an environment sort of like "verbatim" in that it prints each and every unicode character exactly as typed, but... 1> does NOT replicate linebreaks 2> uses the \normalfont 3> justifies, hyphenates and prints the text as LaTeX would normally

I would use it to typeset paragraphs of text from a txt file. (I want it typeset, I don't want it to look like it was printed from notepad or a typewriter)

The text is full of @ # $ % characters that otherwise require special attention to print out, so I figure a lot of folks run into this routinely. Is there a package to help?

Here are most characters (originally copied from the pdf 

http://www.unicode.org/charts/PDF/U0100.pdf

directly from firefox pdf viewer to an emacs buffer) in Unicode Latin
Extended-A, five of them were "not set up for use with LaTeX." and thus I suppressed them.

(don't worry about line numbers)

./myverbatim.tex:209: Package inputenc Error: Unicode char (U+126)

./myverbatim.tex:210: Package inputenc Error: Unicode char (U+127)

./myverbatim.tex:227: Package inputenc Error: Unicode char (U+138)

./myverbatim.tex:244: Package inputenc Error: Unicode char (U+2BC)

./myverbatim.tex:273: Package inputenc Error: Unicode char (U+166)

./myverbatim.tex:274: Package inputenc Error: Unicode char (U+167)
(inputenc)                not set up for use with LaTeX.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.

...

Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

Test of Unicode in verbatim: Ā
ā
Ă
ă
Ą
ą
Ć
ć
Ĉ
ĉ
Ċ
ċ
Č
č
Ď
ď
Đ
đ
Ē
ē
Ĕ
ĕ
Ė
ė
Ę
ę
Ě
ě
Ĝ
ĝ
Ğ
ğ
Ġ
ġ
Ģ
ģ
Ĥ
ĥ
Ĩ
ĩ
Ī
ī
Ĭ
ĭ
Į
į
İ
ı
IJ
ij
Ĵ
ĵ
Ķ
ķ
Ĺ
ĺ
Ļ
ļ
Ľ
ľ
L·
l·
Ł
ł
Ń
ń
Ņ
ņ
Ň
ň
Ŋ
ŋ
Ō
ō
Ŏ
ŏ
Ő
ő
Œ
œ
Ŕ
ŕ
Ŗ
ŗ
Ř
ř
Ś
ś
Ŝ
ŝ
Ş
ş
Š
š
Ţ
ţ
Ť
ť
Ũ
ũ
Ū
ū
Ŭ
ŭ
Ů
ů
Ű
ű
Ų
ų
Ŵ
ŵ
Ŷ
ŷ
Ÿ
Ź
ź
Ż
ż
Ž
ž
s
\end{myverbatim}%$ to stop emacs fontification due to $ and also test if % ok $

Back to \textbf{normal}: $E=mc^2$.
\end{document}

Blockquote


Here is the safer code which requires the full \end{myverbatim} as end tag.

The \end{myverbatim} does not create a paragraph, it can be continued on the very same line with some normal stuff. Similarly the \begin{myverbatim} does not start a paragraph and does not even insert a space if used as \begin{myverbatim}STUFF. Thus this environment myverbatim can be used also for "short verbatim" inside an otherwise normal paragraph.

\makeatletter
\catcode`\{ 12 \catcode`\( 1
    \def\myverbatim@endtagA (end{myver}
\catcode`\{ 1 \catcode`\( 12
\catcode`\} 12 \catcode`\) 2
    \def\myverbatim@endtagB {batim})
\catcode`\} 2 \catcode`\) 12

\def\myverbatim@backslash #1#2#3#4#5#6#7#8#9%
{%
    \def\myverbatim@tempA{#1#2#3#4#5#6#7#8#9}%
    \ifx\myverbatim@tempA\myverbatim@endtagA
          \expandafter\myverbatim@makesure
    \else \@backslashchar\expandafter\myverbatim@tempA
    \fi
}

\def\myverbatim@makesure #1#2#3#4#5#6%
{%
    \def\myverbatim@tempB{#1#2#3#4#5#6}%
    \ifx\myverbatim@tempB\myverbatim@endtagB
          \expandafter\myverbatim@stop
    \fi 
    \@backslashchar\expandafter\myverbatim@tempA\myverbatim@tempB
}

\def\myverbatim@stop\@backslashchar
    \expandafter\myverbatim@tempA\myverbatim@tempB{\end{myverbatim}}

\begingroup
    \lccode`\~ 92
    \lowercase{%
\gdef\myverbatim@makeintelligentbackslash 
   {\catcode92 \active \let~\myverbatim@backslash}%
              }
\endgroup   
\newenvironment{myverbatim}{\let\do\@makeother \dospecials
   \catcode32 10 % normal spaces
   \myverbatim@makeintelligentbackslash % exit properly
   }{}
\makeatother
  • I think that coding should be put into a package and made available on CTAN because it's potentially useful for so many people. Packages aren't required by programmers who can type up code all day long, but they certainly are convenient for non programmers who leisurely use LaTeX. As for me, I'm going to create my own personal package of that. nice work – user12711 Apr 1 '16 at 18:04
  • excellent, this is what I really need: "The \end{myverbatim} does not create a paragraph, it can be continued on the very same line with normal stuff. " – user12711 Apr 1 '16 at 18:58
  • @user12711 I have added to my answer the more robust version which requires \end{myverbatim} and not only \end{myver as closing tag. – user4686 Apr 1 '16 at 19:05
  • @user12711 my approach is not perfect. In certain circumstances it can gobble spaces (when it handles control sequences to check if it is \end{myverbatim}.) I have for personal uses other types of "free-flowing" verbatims. The approach here can be modified to not have this problem, but perhaps it is not worth it to make the extra effort if you only use it with text having some special characters but no backslash. – user4686 Apr 1 '16 at 19:15
  • @user12711 I feel a bit guilty about my original but not perfect approach; with a \verb|...| type of syntax it is much easier; or it is also possible (and arguably better) to mimic more of standard verbatim, but in a free-flowing way, not using typewriter font also. But I wanted to try something new. It works fine, except that encountered control sequences \foo bar1 bar2 will lose the spaces between \foo and bar1 and between bar1 and bar2. – user4686 Apr 1 '16 at 19:49

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.