我有下一个问题:
我在编辑器中复制并粘贴代码后无法编译,因为图片左侧的破折号与右侧的不一样。
但是如果我将“−”替换为“−”,它就可以正常工作(令人惊讶的是,这里粘贴的破折号都是正确的)在这里您可以找到主要来源https://github.com/AnMnv/eBook (第 9 页)
因为在这里粘贴 MWE 是没用的,因为所有虚线都正确粘贴在这个网站上,所以如果您能按照上面的链接打开,我将非常感激电子书.pdf并尝试将第 9 页的示例 4.10 复制到您的编辑器中。
梅威瑟:
\documentclass{article}
\usepackage{scrextend}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[russian,english]{babel}
\usepackage[table]{xcolor}
\usepackage{nicematrix}
\NiceMatrixOptions{cell-space-top-limit=5pt,cell-space-bottom-limit=5pt}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
frame=single,
breaklines=true,
postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
}
\newcommand\realnumberstyle[1]{}
\makeatletter
\newcommand{\zebra}[3]{%
{\realnumberstyle{#3}}%
\begingroup
\lst@basicstyle
\ifodd\value{lstnumber}%
\color{#1}%
\else
\color{#2}%
\fi
\rlap{\hspace*{\lst@numbersep}%
\color@block{\linewidth}{\ht\strutbox}{\dp\strutbox}%
}%
\endgroup
}
\makeatother
\begin{document}
\begin{tabular}{c | c}
\begin{minipage}[m]{0.4\textwidth}
\begin{NiceTabular}{|c|c|c|}
\hline
\cellcolor{red}1& \cellcolor{green}1 & \cellcolor{black!10}EVERY\\\hline
\cellcolor{orange}1 & \cellcolor{red!35}1 & \cellcolor{brown!50}CELL \\ \hline
\cellcolor{green!35}1 & \cellcolor{blue!45}1 & \cellcolor{yellow}CENTERED \\ \hline
\end{NiceTabular}
\end{minipage}
&
\begin{minipage}[m]{0.55\textwidth}
\begin{lstlisting}[numberstyle=\zebra{green!15}{yellow!15},numbers=left,basicstyle=\footnotesize]
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{nicematrix}
\NiceMatrixOptions{cell-space-top-limit=5pt,cell-space-bottom-limit=5pt}
\begin{document}
\begin{table}[htbp]
\centering
\begin{NiceTabular}{|c|c|c|}
\hline
\cellcolor{red}1& \cellcolor{green}1 & \cellcolor{black!10}1 \\ \hline
\cellcolor{orange}1 & \cellcolor{red!35}1 & \cellcolor{brown!50}1 \\ \hline
\cellcolor{green!35}1 & \cellcolor{blue!45}1 & \cellcolor{yellow}1 \\ \hline
\end{NiceTabular}
\end{table}
\end{document}
\end{lstlisting}
\end{minipage}
\end{tabular}
\end{document}
提前致谢。
答案1
listings 使用\textminus
减号,因为-
我看不出如何使用 key=value 接口轻松地阻止它这样做,但您可以在本地重新定义\textminus
为 ASCII,-
以便它在代码中工作:
仅添加一个标记的定义。
\documentclass{article}
\usepackage{scrextend}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[russian,english]{babel}
\usepackage[table]{xcolor}
\usepackage{nicematrix}
\NiceMatrixOptions{cell-space-top-limit=5pt,cell-space-bottom-limit=5pt}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
frame=single,
breaklines=true,
postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
}
\newcommand\realnumberstyle[1]{}
\makeatletter
\newcommand{\zebra}[3]{%
{\realnumberstyle{#3}}%
\begingroup
\lst@basicstyle
\ifodd\value{lstnumber}%
\color{#1}%
\else
\color{#2}%
\fi
\rlap{\hspace*{\lst@numbersep}%
\color@block{\linewidth}{\ht\strutbox}{\dp\strutbox}%
}%
\endgroup
}
\makeatother
\begin{document}
\begin{tabular}{c | c}
\begin{minipage}[m]{0.4\textwidth}
\begin{NiceTabular}{|c|c|c|}
\hline
\cellcolor{red}1& \cellcolor{green}1 & \cellcolor{black!10}EVERY\\\hline
\cellcolor{orange}1 & \cellcolor{red!35}1 & \cellcolor{brown!50}CELL \\ \hline
\cellcolor{green!35}1 & \cellcolor{blue!45}1 & \cellcolor{yellow}CENTERED \\ \hline
\end{NiceTabular}
\end{minipage}
&
\begin{minipage}[m]{0.55\textwidth}
\renewcommand\textminus{\mbox{-}}%<<<<<<<<<<<
\begin{lstlisting}[numberstyle=\zebra{green!15}{yellow!15},numbers=left,basicstyle=\footnotesize]
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{nicematrix}
\NiceMatrixOptions{cell-space-top-limit=5pt,cell-space-bottom-limit=5pt}
\begin{document}
\begin{table}[htbp]
\centering
\begin{NiceTabular}{|c|c|c|}
\hline
\cellcolor{red}1& \cellcolor{green}1 & \cellcolor{black!10}1 \\ \hline
\cellcolor{orange}1 & \cellcolor{red!35}1 & \cellcolor{brown!50}1 \\ \hline
\cellcolor{green!35}1 & \cellcolor{blue!45}1 & \cellcolor{yellow}1 \\ \hline
\end{NiceTabular}
\end{table}
\end{document}
\end{lstlisting}
\end{minipage}
\end{tabular}
\end{document}