最小工作示例

最小工作示例

加载包时caption,它会弄乱使用该包排版的列表的交叉引用showexpl。该\ref命令会打印所在部分的编号LTXexample,而不是列表本身的编号。使用环境时不会出现同样的效果lstlisting。下面的最小工作示例中给出了两者的示例。

有人知道如何解决这个问题,以便LTXexample可以通过数字而不是部分来引用列表?

最小工作示例

代码

\documentclass{article}
\usepackage{showexpl}
\usepackage{caption}
\usepackage{lipsum}
\usepackage{verbatim}

\begin{document}

\section{Lorem ipsum}

\subsection{Dolor sit amet}

\begin{lstlisting}[caption={Listing only using the \texttt{lstlisting} environment.},label={example1}]
\lipsum[1][1]
\end{lstlisting}

\subsection{Consectetuer adipiscing elit}

\verb|\ref{example1}| yields ``\ref{example1}'', which is correct.

\section{Ut purus elit}

\subsection{Vestibulum ut}

\begin{LTXexample}[caption={Listing and output using \texttt{LTXexample} environment.},label={example2}]
  \lipsum[1][2]
\end{LTXexample}

\begin{LTXexample}[caption={Another example of code and output.}, label={example3}]
  \lipsum[1][3]
\end{LTXexample}

\subsection{Placerat ac}

\verb|\ref{example2}| yields ``\ref{example2}'' instead of ``2''.

\verb|\ref{example3}| yields ``\ref{example3}'' instead of ``3''.

\end{document}

输出

在此处输入图片描述

答案1

tcolorbox我建议改为。tcolorboxlistings+showexpllistings包含可轻松显示LaTeX代码和结果的工具。

以下只是defaulttcolorboxes 格式的示例。但可以轻松将其更改为所需的设计。

\documentclass{article}
%\usepackage{showexpl}
%\usepackage{caption}
\usepackage{lipsum}
\usepackage{verbatim}
\usepackage[most]{tcolorbox}

\newtcblisting[auto counter]{myonlylisting}[3][]{%
    title=Listing~\thetcbcounter: #2,
    label=#3,
    listing only,
    #1
}

\newtcblisting[use counter from=myonlylisting]{mylisting}[3][]{%
    title=Listing~\thetcbcounter: #2,
    label=#3,
    text side listing,
    #1 
}

\begin{document}

\section{Lorem ipsum}

\subsection{Dolor sit amet}

\begin{myonlylisting}{Listing only using the \texttt{lstlisting} environment.}{example1}
\lipsum[1][1]
\end{myonlylisting}

\subsection{Consectetuer adipiscing elit}

\verb|\ref{example1}| yields ``\ref{example1}'', which is correct.

\section{Ut purus elit}

\subsection{Vestibulum ut}

\begin{mylisting}{Listing and output using \texttt{LTXexample} environment.}{example2}
  \lipsum[1][2]
\end{mylisting}

\begin{mylisting}{Another example of code and output.}{example3}
  \lipsum[1][3]
\end{mylisting}

\subsection{Placerat ac}

\verb|\ref{example2}| yields ``\ref{example2}'' instead of ``2''.

\verb|\ref{example3}| yields ``\ref{example3}'' instead of ``3''.

\end{document}

在此处输入图片描述

以下代码产生与原始设计类似的风格。

\documentclass{article}
\usepackage{lipsum}
\usepackage{verbatim}
\usepackage[most]{tcolorbox}

\tcbset{
    mystyle/.style={
        empty,
        coltitle=black,
        halign title=center,
        listing options={numbers=left, numberstyle={\tiny}, numbersep=2mm},
        size=fbox,
    }
}

\newtcblisting[auto counter]{myonlylisting}[3][]{%
    mystyle,
    title=Listing~\thetcbcounter: #2,
    label=#3,
    listing only,
    #1,
}

\newtcblisting[use counter from=myonlylisting]{mylisting}[3][]{%
    mystyle,
    title=Listing~\thetcbcounter: #2,
    label=#3,
    text side listing,
    leftlower=5mm,
    overlay={\draw (interior.north west) rectangle (segmentation.south);
        },
    #1 
}

\begin{document}

\section{Lorem ipsum}

\subsection{Dolor sit amet}

\begin{myonlylisting}{Listing only using the \texttt{lstlisting} environment.}{example1}
\lipsum[1][1]
\end{myonlylisting}

\subsection{Consectetuer adipiscing elit}

\verb|\ref{example1}| yields ``\ref{example1}'', which is correct.

\section{Ut purus elit}

\subsection{Vestibulum ut}

\begin{mylisting}{Listing and output using \texttt{LTXexample} environment.}{example2}
\lipsum[1][2]
\end{mylisting}

\begin{mylisting}{Another example of code and output.}{example3}
\lipsum[1][3]
\end{mylisting}

\subsection{Placerat ac}

\verb|\ref{example2}| yields ``\ref{example2}'' instead of ``2''.

\verb|\ref{example3}| yields ``\ref{example3}'' instead of ``3''.

\end{document}

在此处输入图片描述

答案2

据我所知,showexpl钥匙不包括label

放置\label在论据中\caption

\documentclass{article}
\usepackage{showexpl}
\usepackage{caption}
\usepackage{lipsum}
\usepackage{verbatim}

\begin{document}

\section{Lorem ipsum}

\subsection{Dolor sit amet}

\begin{lstlisting}[
  caption={Listing only using the \texttt{lstlisting} environment.},
  label={example1}
]
\lipsum[1][1]
\end{lstlisting}

\subsection{Consectetuer adipiscing elit}

\verb|\ref{example1}| yields ``\ref{example1}'', which is correct.

\section{Ut purus elit}

\subsection{Vestibulum ut}

\begin{LTXexample}[
  caption={Listing and output using \texttt{LTXexample} environment.\label{example2}}
]
  \lipsum[1][2]
\end{LTXexample}

\begin{LTXexample}[caption={Another example of code and output.\label{example3}}]
  \lipsum[1][3]
\end{LTXexample}

\subsection{Placerat ac}

\verb|\ref{example2}| yields ``\ref{example2}'' instead of ``2''.

\verb|\ref{example3}| yields ``\ref{example3}'' instead of ``3''.

\end{document}

在此处输入图片描述

相关内容