我试图在 TeX 语言列表中转义美元符号,但不起作用。我试过这个帖子但没有成功。
这是 MWE:
\documentclass[border={10pt 10pt 10pt 10pt}]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
% Python style for highlighting
\newcommand\latexstyle{\lstset{
language=TeX,
basicstyle=\scriptsize\ttfamily,
mathescape=true,
keywordstyle=\color{MidnightBlue}\bfseries,
morekeywords={begin, addplot, addlegendentry, documentclass, usepackage, usetikzlibrary, usepgfplotslibrary, pgfplotsset, nextlist, coordinate small, draw},
}}
% Python environment
\lstnewenvironment{latex}[1][mathescape]
{
\latexstyle
\lstset{#1}
}
{}
\begin{document}
\begin{latex}
\begin{tikzpicture}
\begin{loglogaxis}
\addplot+ [ultra thick, dotted] table[]{
10 0.2
100 0.1
1000 0.05}
coordinate [pos=0.15] (A)
coordinate [pos=0.25] (B);
\addlegendentry{legend 1}
\coordinate (A') at ($(A)!3pt!90:(B)$);
\coordinate (B') at ($(B)!3pt!270:(A)$);
\draw (A')-|(B') node[pos=0.25,above]{$1$}
node[pos=0.75,right]{$\alpha$};
\draw [shorten <=-3pt,shorten >=-3pt] (A') -- (B');
\end{loglogaxis}
\end{tikzpicture}
\end{latex}
\end{document}
答案1
您设置了mathescape=true
两次。您可能误解了它的含义:使用mathescape=true
,listings
当您输入类似以下内容时,将表现得好像是 TeX 数学公式$\alpha$
(因此打印数学字母)。我认为您在排版这样的列表时不需要它。
\documentclass[border={10pt 10pt 10pt 10pt}]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
% Python style for highlighting
\lstdefinestyle{latexstyle}{
language=TeX,
basicstyle=\scriptsize\ttfamily,
% mathescape=true,
keywordstyle=\color{MidnightBlue}\bfseries,
morekeywords={
begin,
addplot,
addlegendentry,
documentclass,
usepackage,
usetikzlibrary,
usepgfplotslibrary,
pgfplotsset,
nextlist,
coordinate small,
draw
},
}
% latexn environment
\lstnewenvironment{latex}[1][]
{\lstset{style=latexstyle,#1}}
{}
\begin{document}
\begin{latex}
\begin{tikzpicture}
\begin{loglogaxis}
\addplot+ [ultra thick, dotted] table[]{
10 0.2
100 0.1
1000 0.05}
coordinate [pos=0.15] (A)
coordinate [pos=0.25] (B);
\addlegendentry{legend 1}
\coordinate (A') at ($(A)!3pt!90:(B)$);
\coordinate (B') at ($(B)!3pt!270:(A)$);
\draw (A')-|(B') node[pos=0.25,above]{$1$}
node[pos=0.75,right]{$\alpha$};
\draw [shorten <=-3pt,shorten >=-3pt] (A') -- (B');
\end{loglogaxis}
\end{tikzpicture}
\end{latex}
\end{document}