我想水平缩放列表代码中的每个字符,使其变窄。该basewidth
选项不是我想要的,因为它不缩放字符。有什么方法可以实现吗?
\documentclass{article}
\usepackage{listings,tcolorbox,arev}
\tcbuselibrary{listings}
\lstset{
basicstyle=\ttfamily,
}
\begin{document}
\begin{lstlisting}
Hello \LaTeX{} World! Hello \LaTeX{} World!
\end{lstlisting}
\begin{tcblisting}{listing and text}
Hello \LaTeX{} World! Hello \LaTeX{} World!
\end{tcblisting}
\lstset{
basicstyle=\ttfamily,
basewidth = {0.5em,0.45em},
}
\begin{lstlisting}
Hello \LaTeX{} World! Hello \LaTeX{} World!
\end{lstlisting}
\begin{tcblisting}{listing and text,listing options={basewidth = {0.5em,0.45em}}}
Hello \LaTeX{} World! Hello \LaTeX{} World!
\end{tcblisting}
\end{document}
答案1
listings
例如,这种方法不是,因此它没有字符突出显示。相反,它是tokcycle
,我在这里做的是手动压缩字体的不可饶恕的罪过(我错了!)。我还消除了去标记化宏后面的那些讨厌的空格,当然,如果后面跟着 cat-11 标记,情况除外。
前三行显示连续压缩的列表
第四个例子演示了自动换行。
\obeylines
最后,在第 5 个示例中,我展示了如何将该方法与and/or结合起来,\obeyspaces
以列出源代码后面的换行符和/或空格。
如您所见,该方法即使带有参数,也是逐字的。但是,它仍然需要平衡括号。
\documentclass{article}
\usepackage{graphicx,tokcycle}
\usepackage[T1]{fontenc}
\newsavebox\sptok
\sbox\sptok{ }
\newif\ifmacro
\newcommand\altdetok[2]{\begingroup\stripgroupingtrue\macrofalse
\stripimplicitgroupingcase{-1}%
\tokcycle
{\ifmacro\def\tmp{##1}\ifcat\tmp A\scalebox{#2}[1]{ }%
\fi\allowbreak\macrofalse\fi
\scalebox{#2}[1]{\string##1}}
{\ifmacro\macrofalse\fi\{\processtoks{##1}\}\allowbreak}
{\tctestifx{\par##1}{\\}{%
\allowbreak\scalebox{#2}[1]{\string##1}\macrotrue
}}
{\hspace{#2\wd\sptok plus 3em minus .3ex}}
{#1}\endgroup}
\catcode37=12
\newcommand\narrowdetok{\catcode37=12\narrowdetokaux}
\catcode37=14
\newcommand\narrowdetokaux[2][1]{\par\noindent
\altdetok{#2}{#1}\catcode37=14\par}
\begin{document}
\narrowdetok{Today is \today and \today! Here is a
\rule{1em}{1ex}.}
\narrowdetok[.8]{Today is \today and \today! Here is a
\rule{1em}{1ex}.}
\narrowdetok[.6]{Today is \today and \today! Here is a
\rule{1em}{1ex}.}
\noindent\hrulefill
\narrowdetok[.7]{Today is \today! Here is a \rule{1em}{1ex}.
I will continue with this &^#$%\ listing to see if it
autobreaks at each line or if it obeys lines.
New paragraph.}
\noindent\hrulefill\obeylines\obeyspaces
\narrowdetok[.7]{Today is \today! Here is a \rule{1em}{1ex}.
I will continue with this &^#$%\ listing to see if it
autobreaks at each line or if it obeys lines.
New paragraph.}
\end{document}