您好,如何在代码块中放置下标?
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{float}
\usepackage{amsmath}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}
\usepackage{graphicx}
\graphicspath{ {./images/} }
\colorlet{mygray}{black!30}
\colorlet{mygreen}{green!60!blue}
\colorlet{mymauve}{red!60!blue}
\parindent 0px
\lstset{
backgroundcolor=\color{gray!10},
basicstyle=\ttfamily,
columns=fullflexible,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{mygreen},
extendedchars=true,
frame=single,
keepspaces=true,
keywordstyle=\color{blue},
language=c++,
numbers=none,
numbersep=5pt,
numberstyle=\tiny\color{blue},
rulecolor=\color{mygray},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=5,
stringstyle=\color{mymauve},
tabsize=3,
title=\lstname
}
\begin{document}
\begin{lstlisting}
Test_k
\end{lstlisting}
\end{document}
答案1
mathescape
最简单的方法可能是使用命令中的选项\lstset
。
\lstset{
mathescape,
backgroundcolor=\color{gray!10},
basicstyle=\ttfamily,
columns=fullflexible,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{mygreen},
extendedchars=true,
frame=single,
keepspaces=true,
keywordstyle=\color{blue},
language=c++,
numbers=none,
numbersep=5pt,
numberstyle=\tiny\color{blue},
rulecolor=\color{mygray},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=5,
stringstyle=\color{mymauve},
tabsize=3,
title=\lstname
}
\begin{document}
\begin{lstlisting}
Test$_k$
Test$^k$
\end{lstlisting}
\end{document}
这将为您提供: