列表中的固定宽度和比例打字机字体之间有什么区别?

列表中的固定宽度和比例打字机字体之间有什么区别?

在下面的 MWE 中,我想知道为什么pcr(Courier) 字体在转义和非转义版本中以及对于任何列设置都对齐{listing}- 而cmtt(Computer Modern Teletype) 仅对fullflexible列进行转义和非转义对齐。MWE 基本上是针对不同字体重复的相同代码(我本来会将其存储在宏中,但我会得到“列表开始后文本被丢弃...”):

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor} % \pagecolor
\pagecolor{yellow!15}
\setlength{\fboxsep}{0pt}

\begin{document}


\renewcommand{\ttdefault}{pcr}

Here entering a sample paragraph with \texttt{some} words entered in
\texttt{typewriter font}, which should also be \texttt{monospaced} - or rather,
\texttt{fixed width}. It can be quite useful for showing \texttt{variables}.

\noindent\begin{minipage}[t]{.325\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex1a,
columns=fixed,
frame=tlrb]
080484c4 <list>:
 !\fbox{\hspace{-\fboxrule}\texttt{80484c4:\fbox{ }cmd one}\linebreak}!
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}
\hspace{1cm}
\noindent\begin{minipage}[t]{.325\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex1b,
columns=fullflexible,
frame=tlrb]
080484c4 <list>:
 !\fbox{\hspace{-\fboxrule}\texttt{80484c4:\fbox{ }cmd one}\linebreak}!
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}



\renewcommand{\ttdefault}{cmtt}

Here entering a sample paragraph with \texttt{some} words entered in
\texttt{typewriter font}, which should also be \texttt{monospaced} - or rather,
\texttt{fixed width}. It can be quite useful for showing \texttt{variables}.

\noindent\begin{minipage}[t]{.325\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex2a,
columns=fixed,
frame=tlrb]
080484c4 <list>:
 !\fbox{\hspace{-\fboxrule}\texttt{80484c4:\fbox{ }cmd one}\linebreak}!
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}
\hspace{1cm}
\noindent\begin{minipage}[t]{.325\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex2b,
columns=fullflexible,
frame=tlrb]
080484c4 <list>:
 !\fbox{\hspace{-\fboxrule}\texttt{80484c4:\fbox{ }cmd one}\linebreak}!
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}


\end{document}

...输出如下:

测试01a.png

现在这对我来说很奇怪:固定的列,80484c对于字体,转义和未转义的列表文本在字符之间具有完全不同的间距(比较清单 3 中的部分)cmtt;对于 ,不会发生这种情况pcr

因此,我由此得出的结论是, 显然pcr是一种“真正的”固定宽度字体,而 则cmtt“只是”一种“打字机”字体,但却是成比例的字体?但首先,cmtt在我看来, 看起来并不那么成比例 - 即使成比例,它也应该对齐columns=fixed,不是吗?

我的问题是:

  • 有没有一种(简单的)方法可以在 Latex 中确定字体是否真的是固定宽度的?(例如我想输入类似的内容\iffontfixedwidth{cmtt}\typeout{yes}\else\typeout{no}\fi
  • 我是否可以以某种方式表现得与 中cmtt相同(即,无论转义/非转义或设置如何,字母之间具有相同的对齐方式/间距)?pcr{listing}columns=

答案1

使用columns=fixedcolumns=flexible,列表构建时将每个字符插入到 0.6em 宽的框中fixed, 0.45em 宽的框中flexible(可以通过 更改默认值basewidth)。

当字体具有固定宽度的字形(如 Courier 或 Computer Modern Typewriter)时,使用这两种类型是没有意义的,因为对于这些字体,单词之间的空间与每个字符的宽度相同,因此列会自动对齐。

在这些情况下,columns=fullflexible是最好的:字符不会插入框中并且对齐是自动的。

如何将 设置basewidth为合理的值fixed?您可以利用这样一个事实:basewidth计算 时,基本字体已被选择,因此

basewidth=\fontcharwd\font`W,

将此宽度设置为(通常)最宽字符的宽度。

例子:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{xcolor} % \pagecolor
\pagecolor{yellow!15}
\setlength{\fboxsep}{0pt}

\begin{document}

Here entering a sample paragraph with \texttt{some} words entered in
\texttt{typewriter font}, which should also be \texttt{monospaced} - or rather,
\texttt{fixed width}. It can be quite useful for showing \texttt{variables}.

\noindent\begin{minipage}[t]{.4\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex2a,
columns=fixed,
basewidth=\fontcharwd\font`M,
frame=tlrb]
080484c4 <list>:
 !\fbox{\hspace{-\fboxrule}\texttt{80484c4:\_cmd one}\linebreak}!
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}
\hspace{1cm}
\noindent\begin{minipage}[t]{.4\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex2b,
columns=fullflexible,
keepspaces,
frame=tlrb]
080484c4 <list>:
 !\fbox{\hspace{-\fboxrule}\texttt{80484c4:\_cmd one}}!
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}


\end{document}

在此处输入图片描述

如何确定字体是否是固定宽度?

\ifdim\fontcharwd\font`i=\fontcharwd\font`W
   <code for fixed width font>
\else
   <code for proportional font>
\fi

当然\font是指现在的字体。

答案2

好的,我设法找到了一些方法,但不确定它是否正确。基本上,这都是关于设置basewidth=,但设置为正确的值,这是我通过使用\widthof包获得的calc。现在,问题是我们必须有一个可扩展的宏basewidth=- 而且,我认为在basewidth=处理时,{lstlisting}环境仍然“不知道”\lst@basicstyle已设置为什么。这意味着,必须在{lstlisting}开始之前计算这个长度,并且必须在那里重新输入将用作的参数basicstyle=。为此,下面我使用了一个新命令。

因此,不要粘贴整个示例,这些是一开始最实质性的变化:

\documentclass{article}

\usepackage{calc}%\widthof
\usepackage{listings}
\usepackage{xcolor} % \pagecolor
\pagecolor{yellow!15}
\setlength{\fboxsep}{0pt}

\newlength{\tlen}
% \xdef\etlen{}
\makeatletter
\newcommand{\getMyLen}[1]{%
%   \setlength{\tlen}{\widthof{{\lst@basicstyle W}}}%
  \setlength{\tlen}{\widthof{#1}}%
%   \xdef\etlen{\the\tlen}%
%   \etlen% return
  \the\tlen% return
}
\makeatother
\begin{document}


\renewcommand{\ttdefault}{pcr}

Here entering a sample paragraph with \texttt{some} words entered in
\texttt{typewriter font}, which should also be \texttt{monospaced} - or rather,
\texttt{fixed width}. It can be quite useful for showing \texttt{variables};
\getMyLen{\scriptsize\ttfamily W}.

\noindent\begin{minipage}[t]{.325\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,
caption={[short] Some instructions here; the font here is \texttt{\ttdefault}.},
escapechar=!,
showlines=true,
label=lst:ex1a,
columns=fixed,
basewidth=\tlen,
frame=tlrb]
080484c4 <list>:
 !\fbox{\hspace{-\fboxrule}\texttt{80484c4:\fbox{ }cmd one}\linebreak}!
 80484c7: cmd two
 80484ca: cmd three, four
 80484cf: cmd five
 80484d6: cmd six, seven
 80484dd: cmd more than enough
 80484e0: cmd  not_even_joking
\end{lstlisting}
\end{minipage}
\hspace{1cm}
...
\renewcommand{\ttdefault}{cmtt}

Here entering a sample paragraph with \texttt{some} words entered in
\texttt{typewriter font}, which should also be \texttt{monospaced} - or rather,
\texttt{fixed width}. It can be quite useful for showing \texttt{variables};
\getMyLen{\scriptsize\ttfamily W}.
...

... 否则basewidth=\tlen,对所有都添加{lstlisting};最终结果是这样的:

测试01b.png

... 可以说,在我看来,无论哪种字体看起来都很好。当然,如何确定字体是否真的是等宽字体的问题仍然悬而未决...

相关内容