不确定为什么我没有解决这个问题,但我似乎无法在代码部分获得固定宽度的字体。我认为关于如何更改代码字体的问题可以解决问题,但对我来说没用。
对我来说,以下代码会产生未对齐的输出(&
代码中未对齐):
\documentclass{article}
\usepackage{amsmath}
\usepackage{showexpl}
\usepackage{xcolor}
\usepackage{multirow} % Newly added for 2nd example.
\lstset{
backgroundcolor=\color{yellow},
basicstyle=\small\ttfamily,% print whole listing small
keywordstyle=\color{blue}\bfseries\underbar,
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
%columns=fixed,
%commentstyle=\color{red},
showstringspaces=false
}
\lstloadlanguages{[LaTeX]TeX}
\begin{document}
Here is an example of LaTeX code and its output:
\begin{LTXexample}[width=0.40\linewidth,preset=\vspace{1.5mm}]
\begin{alignat*}{4}
y &= -4 &+ 3 &+4 &-7 \\
y &= &+ 3 & &-7 \\
\intertext{Therefore}
a &= b &d &= cccc &e &= d \\
a &= bbbb &d &= c &e &= d
\end{alignat*}
\end{LTXexample}
\begin{LTXexample}[width=0.40\linewidth,preset=\vspace{1.5mm},rframe={},pos=b]
\begin{tabular}{|l|l|l|l|}
\multicolumn{4}{c}{Dimensions} \\
\multirow{4}{*}{Style}
&\multirow{2}{*}{Portrait} & Width \\
& & Height \\
&\multirow{2}{*}{Landscape}& Width \\
& & Height \\
\end{tabular}
\end{LTXexample}
\end{document}
答案1
问题是,带有“固定列”的列表的默认设置是 0.6em 宽,而 Computer Modern Typewriter 中的字符是 0.5em 宽。因此,您只需添加选项
columns=fixed,basewidth=.5em,
到\lstset
。
如果不了解字符宽度,也可以这样说
\sbox0{\small\ttfamily A}
\edef\mybasewidth{\the\wd0 }
\lstset{
basicstyle=\small\ttfamily,% print whole listing small
columns=fixed,basewidth=\mybasewidth,
...}
答案2
你应该使用好看的打字机字体,比如“beramono”
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.82]{beramono}
\usepackage{microtype}
\usepackage{amsmath}
\usepackage{showexpl}
\usepackage{xcolor}
\usepackage{multirow} % Newly added for 2nd example.
\lstset{
backgroundcolor=\color{yellow},
basicstyle=\small\ttfamily\SetTracking{encoding=*}{-60}\lsstyle,
basewidth=0.55em,
keywordstyle=\color{blue}\bfseries\underbar,
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
%columns=fixed,
%commentstyle=\color{red},
showstringspaces=false}
\lstloadlanguages{[LaTeX]TeX}
\begin{document}
Here is an example of LaTeX code and its output:
\begin{LTXexample}[width=0.40\linewidth,preset=\vspace{1.5mm}]
\begin{alignat*}{4}
y &= -4 &+ 3 &+4 &-7 \\
y &= &+ 3 & &-7 \\
\intertext{Therefore}
a &= b &d &= cccc &e &= d \\
a &= bbbb &d &= c &e &= d
\end{alignat*}
\end{LTXexample}
\begin{LTXexample}[width=0.40\linewidth,preset=\vspace{1.5mm},rframe={},pos=b]
\begin{tabular}{|l|l|l|l|}
\multicolumn{4}{c}{Dimensions} \\
\multirow{4}{*}{Style}
&\multirow{2}{*}{Portrait} & Width \\
& & Height \\
&\multirow{2}{*}{Landscape}& Width \\
& & Height \\
\end{tabular}
\end{LTXexample}
\end{document}