显示用于列出环境(如数学环境)的标签

显示用于列出环境(如数学环境)的标签

我正在使用更好的排版代码,该代码最初由于期刊提交要求而listings被放置在不理想的环境中(每行verbatims里面都有负空间)。eqnarray

但是,使用数学环境的一个优点equation是,您可以在右侧获得自动或手动标记。我想用 来重现这一点listings,而不需要标题。

以下是 MWE:

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{color}
\usepackage{hyperref}
\usepackage{listings}

% slightly nicer listings output
\lstset{%
  basicstyle=\ttfamily\footnotesize,%
  breakatwhitespace=true,%
  breaklines=false,%
  frame=none,%
  numberbychapter=false,%
  numbers=left,%
  numberstyle=\tiny,
}

\begin{document}

\textcolor{blue}{1st test: aligned verbatims in equation}
\begin{equation}
  \label{snip:1}
  \begin{aligned}
    &\verb!import numpy, psi4! \\
    &\verb!np_array = numpy.zeros((5, 5))! \\
    &\verb!psi4_matrix = psi4.core.Matrix.from_array(np_array)! \\
    &\verb!new_np_array = numpy.array(psi4_matrix)!
  \end{aligned}
\end{equation}

\textcolor{blue}{2nd test: aligned verbatims in eqnarray}
\begin{eqnarray}
  \begin{aligned}
    &\verb!import numpy, psi4! \\
    &\verb!np_array = numpy.zeros((5, 5))! \\
    &\verb!psi4_matrix = psi4.core.Matrix.from_array(np_array)! \\
    &\verb!new_np_array = numpy.array(psi4_matrix)!
  \end{aligned}
  \label{snip:2}
\end{eqnarray}

\textcolor{blue}{3rd test: listing without caption}
\begin{lstlisting}[label=snip:3]
import numpy, psi4
np_array = numpy.zeros((5, 5))
psi4_matrix = psi4.core.Matrix.from_array(np_array)
new_np_array = numpy.array(psi4_matrix)
\end{lstlisting}

\textcolor{blue}{4th test: listing with caption}
\begin{lstlisting}[label=snip:4,caption=I am an unwanted caption.]
import numpy, psi4
np_array = numpy.zeros((5, 5))
psi4_matrix = psi4.core.Matrix.from_array(np_array)
new_np_array = numpy.array(psi4_matrix)
\end{lstlisting}

Using standard \verb!\ref{label}!:
\begin{itemize}
\item The first test is \ref{snip:1}.
\item The second test is \ref{snip:2}.
\item The third test is \ref{snip:3}.
\item The fourth test is \ref{snip:4}.
\end{itemize}

Using \verb!\autoref{label}! from \texttt{hyperref}:
\begin{itemize}
\item The first test is \autoref{snip:1}.
\item The second test is \autoref{snip:2}.
\item The third test is \autoref{snip:3}.
\item The fourth test is \autoref{snip:4}.
\end{itemize}

\end{document}

输出为:

在此处输入图片描述 在此处输入图片描述

listings对于没有标题的实例,似乎根本不需要计数器。如何才能获得数学环境的外观和行为listings,而无需标题?

答案1

我不知道这是否是一个可接受的解决方案,但您可以使用tcolorbox没有标题的,并将listings其放入其中,minipage然后放入适当的宏中以打印标签。

\documentclass[12pt]{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting[auto counter]{mylist}[1][]{%
    title={},
    colback=white,
    colframe=white,
    listing only,
    enhanced,
    sharp corners,
    bottom=0pt,
    top=0pt,
    boxsep=0pt,
    leftrule=0pt,
    left=10pt,
    listing engine=listings,
    listing options = {
        basicstyle=\ttfamily\footnotesize,%
        breakatwhitespace=true,%
        breaklines=false,%
        frame=none,%
        numberbychapter=false,%
        numbers=left,%
        numberstyle=\tiny,
        numbersep=6pt,
    },
    #1 
}
\makeatletter
\newcommand{\tcb@cnt@mylistautorefname}{Listing}
\newcommand{\printlabel}{\begin{minipage}{.05\linewidth}\raggedleft(\makeatletter\thetcb@cnt@mylist\makeatother)\end{minipage}}
\makeatother

\usepackage{hyperref}

\begin{document}
    You could use a \verb|tcolorbox| whithin a \verb|minipage| which is a little smaller than \verb|\linewidth| and write the (label) automatically side by side with an appropriate command. Please note that I didn't put spaces before the code.

    \noindent\begin{minipage}{.95\linewidth}
    \begin{mylist}[label=snip:4]
import numpy, psi4
np_array = numpy.zeros((5, 5))
psi4_matrix = psi4.core.Matrix.from_array(np_array)
new_np_array = numpy.array(psi4_matrix)
    \end{mylist}
    \end{minipage}
    \printlabel

    Using standard \verb!\ref{label}!:
    now is \ref{snip:4}.

    Using \verb!\autoref{label}! from \texttt{hyperref}:
    Now is \autoref{snip:4}.

\end{document}

在此处输入图片描述

相关内容