我正在寻找一个 LaTeX 包,它可以实现一种类似于“逐字”的环境,即它可以精确地打印出输入的每个 Unicode 字符,但是…… 1> 不会复制换行符 2> 使用 \normalfont 3> 对齐、连字符并打印文本,就像 LaTeX 通常所做的那样
我会用它来排版 txt 文件中的文本段落。(我希望它排版,我不希望它看起来像是从记事本或打字机打印出来的)
文本中充满了 @ # $ % 字符,否则打印时需要特别注意,所以我认为很多人经常遇到这种情况。 有没有可以帮助的软件包?
答案1
如果您不需要文本中的命令来排版,可以使用 jfbu 的宏的简化版本。将环境转变为“输入”类型的宏也很容易。
与 jfbu 的宏一样,此代码依赖于使用 T1 编码字体。
\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}
答案2
您不需要为此准备包裹。
答案底部的更新是为了稍微复杂一些的编码,它\end{myverbatim}
不仅需要\end{myver
决定环境的结束是否已经被达到。
\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}
这是更安全的代码,需要完整的\end{myverbatim}
as 结束标签。
不会\end{myverbatim}
创建段落,但可以在同一行继续显示一些正常内容。同样,
\begin{myverbatim}
如果用作 , 也不会开始段落,甚至不会插入空格\begin{myverbatim}STUFF
。因此,此环境myverbatim
还可用于在其他正常段落内显示“简短逐字内容”。
\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