根据两列中的位置设置不同的环境设置(也考虑图形环境)

根据两列中的位置设置不同的环境设置(也考虑图形环境)

我询问了使用列表环境根据位置将行号放在左侧或右侧。

根据两列中的位置设置不同的环境设置

代码运行正常,但是当我将环境放在图形环境中时,行号的方向就错了。

在此处输入图片描述

考虑到浮动环境中的情况,是否有可能找到文档的正确一侧?我需要浮动环境来为源代码制作标题。

这是代码:

% https://tex.stackexchange.com/questions/286592/setting-different-environment-setup-depending-on-the-location-in-two-column
\documentclass[conference]{IEEEtran}
\usepackage{listings}
\usepackage{lipsum}

\lstdefinelanguage{rruby}{
        escapechar=\,
        basicstyle=\scriptsize\ttfamily,
        numberstyle=\scriptsize\ttfamily,
        stepnumber=1,
        numbersep=3pt,
        showstringspaces=false,
        breaklines=true,
        frame=lines,
        %backgroundcolor=\color{background},
        literate=
         *{0}{{{\color{numb}0}}}{1}
            {1}{{{\color{numb}1}}}{1}
            {2}{{{\color{numb}2}}}{1}
            {3}{{{\color{numb}3}}}{1}
            {4}{{{\color{numb}4}}}{1}
            {5}{{{\color{numb}5}}}{1}
            {6}{{{\color{numb}6}}}{1}
            {7}{{{\color{numb}7}}}{1}
            {8}{{{\color{numb}8}}}{1}
            {9}{{{\color{numb}9}}}{1}
            {:}{{{\color{punct}{:}}}}{1}
            {,}{{{\color{punct}{,}}}}{1}
            {\{}{{{\color{delim}{\{}}}}{1}
            {\}}{{{\color{delim}{\}}}}}{1}
            {[}{{{\color{delim}{[}}}}{1}
            {]}{{{\color{delim}{]}}}}{1},
            }

\lstset{
    basicstyle=\ttfamily\scriptsize,
}
\makeatletter
\lstnewenvironment{ruby}{
        \if@firstcolumn       
            \lstset{emph={def, class, end, typedef, type, constraint, sentence},emphstyle=\textbf, language=rruby, numbers=left}
        \else
            \lstset{emph={def, class, end, typedef, type, constraint, sentence},emphstyle=\textbf, language=rruby, numbers=right}
        \fi
}{}
\makeatother


\begin{document}

\lipsum

\begin{figure}[htbp]
\begin{ruby}
...
\end{ruby}
\end{figure}

\lipsum

\begin{figure}[htbp]
\begin{ruby}
...
\end{ruby}
\end{figure}

\end{document}

答案1

为了制作标题,我可以使用 caption 包。提示来自无浮动的标签和标题

\documentclass[conference]{IEEEtran}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{color}

\DeclareCaptionType{mytype}[Code][List of mytype]
\newenvironment{code}{}{}
\definecolor{delim}{RGB}{20,105,176}

\lstdefinelanguage{rruby}{
        escapechar=\,
        basicstyle=\scriptsize\ttfamily,
        numberstyle=\scriptsize\ttfamily,
        stepnumber=1,
        numbersep=3pt,
        showstringspaces=false,
        breaklines=true,
        frame=lines,
        %backgroundcolor=\color{background},
        literate=
         *{0}{{{\color{numb}0}}}{1}
            {1}{{{\color{numb}1}}}{1}
            {2}{{{\color{numb}2}}}{1}
            {3}{{{\color{numb}3}}}{1}
            {4}{{{\color{numb}4}}}{1}
            {5}{{{\color{numb}5}}}{1}
            {6}{{{\color{numb}6}}}{1}
            {7}{{{\color{numb}7}}}{1}
            {8}{{{\color{numb}8}}}{1}
            {9}{{{\color{numb}9}}}{1}
            {:}{{{\color{punct}{:}}}}{1}
            {,}{{{\color{punct}{,}}}}{1}
            {\{}{{{\color{delim}{\{}}}}{1}
            {\}}{{{\color{delim}{\}}}}}{1}
            {[}{{{\color{delim}{[}}}}{1}
            {]}{{{\color{delim}{]}}}}{1},
            }

\lstset{
    basicstyle=\ttfamily\scriptsize,
}
\makeatletter
\lstnewenvironment{ruby}{
        \if@firstcolumn       
            \lstset{emph={def, class, end, typedef, type, constraint, sentence},emphstyle=\textbf, language=rruby, numbers=left}
        \else
            \lstset{emph={def, class, end, typedef, type, constraint, sentence},emphstyle=\textbf, language=rruby, numbers=right}
        \fi
}{}
\makeatother


\begin{document}
\lipsum

\begin{code}
\begin{ruby}
...
\end{ruby}
\captionof{mytype}{Something in my type}
\end{code}

\begin{lstlisting}[language=ruby, numbers=left,firstnumber=1]
...
\end{lstlisting}
\end{document}

在此处输入图片描述

相关内容