`listings` 和 `sidecap` - 列表的侧边标题

`listings` 和 `sidecap` - 列表的侧边标题

我正在使用该包sidecap来显示图片/表格左侧或右侧的标题,如下所示:

\documentclass[a4paper,twoside,11pt,openright]{scrbook}     
\usepackage{graphicx}           
\usepackage[wide]{sidecap}                                      
\usepackage[font=footnotesize,
    format=plain,
    labelfont={bf,sf},
    textfont={it},
    width=10pt]{caption}

\newcommand{\fig}[4]{
    \begin{SCfigure}
      \centering
            \includegraphics[width=\textwidth]{#1}
      \caption[#2]{#3}
      \label{fig:#4}
    \end{SCfigure}
}

并且,在文档中:

\begin{document}
\fig{background_flickr.png}{Flickr geotagging functionality}{Flickr geotagging functionality. By navigating and panning on a map, the user can place an image or video and specify the desired level of spatial granularity.}{background_flickr}
\end{document}

得出的结果是:

在此处输入图片描述

我也使用该包listings进行代码编写,设置如下:

\usepackage{listings}
\lstset{
   backgroundcolor=\color{lightgray},
   extendedchars=true,
   basicstyle=\footnotesize\ttfamily,
   xleftmargin=20pt,
   showstringspaces=false,
   showspaces=false,
   numbers=left,
   numberstyle=\footnotesize,
   tabsize=2,
   breaklines=true,
   showtabs=false,
   captionpos=tb
}

插入如下代码清单

\begin{lstlisting}[caption={[JSON Example]A basic JSON example}, label=src:DataTwitterAPIJSON]
{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    },
    "phoneNumbers": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}
\end{lstlisting}

产量:

在此处输入图片描述

有两个问题:首先,标题根本无法正确显示(似乎非常窄)。其次,我需要它在左页上位于列表的左侧,在右页上位于列表的右侧(就像sidecap上面的正常行为一样)。我怀疑“狭窄”的外观在某种程度上来自包sidecap。有没有办法不仅可以用于sidecap图表,还可以用于列表?

答案1

这里似乎发生了几件事。

首先,您没有利用sidecap列表环境的软件包。其次,您告诉环境listings标题应该放在顶部和底部——因此您可能会遇到某种冲突。

这里的解决方案不是最佳的: 它不允许你的listings环境跨越页面边界。但考虑到你想把它当作一个图形,这似乎不算太糟。

我在这里所做的是将 放置在listings环境minipageSCfigureminipage设置为当前页面的文本宽度。

\documentclass[a4paper,twoside,11pt,openright]{scrbook}     
\usepackage{xcolor}
\usepackage{graphicx}

%% setting up new float
\usepackage{float}
\floatname{mylisting}{Listing}
\newfloat{mylisting}{tbhp}{lst}[chapter]
\newcommand{\listoflistings}{\listof{mylisting}{List of Listings}}

%% setting up side captions
\usepackage[wide]{sidecap}                                      
\makeatletter
\@ifdefinable\SC@listings@vpos{\def\SC@listings@vpos{b}}
\newenvironment{SClisting}{\SC@float[\SC@listings@vpos]{mylisting}}{\endSC@float}
\newenvironment{SClisting*}{\SC@dblfloat[\SC@listings@vpos]{mylisting}}{\endSC@dblfloat}
%% `sidecap` and `float` access the name of the new float differently.
%% You can't rely on `float` to pass `sidecap` the correct name.  So,
%% here we manually feed the correct name for the new float in a manner 
%% pleasing to `sidecap`.
\@namedef{mylistingname}{Listing}
\makeatother

%% setting up formatting for cpations
\usepackage
  [
    font=footnotesize,
    format=plain,
    labelfont={bf,sf},
    textfont={it},
    width=10pt
  ]{caption}

%% creating figure command
\newcommand{\fig}[4]{%
  \begin{SCfigure}
    \centering
      \includegraphics[width=\textwidth]{#1}
    \caption[#2]{#3}
    \label{fig:#4}
  \end{SCfigure}%
}

%% setting up listings
\usepackage{listings}
\lstset
  {
    backgroundcolor=\color{gray!20},
    extendedchars=true,
    basicstyle=\footnotesize\ttfamily,
    xleftmargin=20pt,
    showstringspaces=false,
    showspaces=false,
    numbers=left,
    numberstyle=\footnotesize,
    tabsize=2,
    breaklines=true,
    showtabs=false,
  }

\begin{document}
%% Uncomment next line to get a list of `listings` floats
%% \listoflistings
%%
%% The following was here just for testing how `float` and `sidecap`
%% handle labeling the caption.  You don't need these lines.
%% \begin{mylisting}
%%   \centering
%%     Hello
%%   \caption{Hello}
%% \end{mylisting}

\fig{example-image-a}
    {Flickr geotagging functionality}
    {Flickr geotagging functionality. By navigating and panning on a map,
     the user can place an image or video and specify the desired level of
     spatial granularity.}
    {background_flickr}

\begin{SClisting}
\begin{minipage}[t]{\textwidth}
\begin{lstlisting} 
{
  "firstName": "John",
  "lastName": "Smith",
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "fax",
      "number": "646 555-4567"
    }
  ]
}
\end{lstlisting}
\end{minipage}
  \caption[JSON Example]{A basic JSON example}
  \label{fig:src:DataTwitterAPIJSON}
\end{SClisting}

\end{document}

在此处输入图片描述

我的另一个想法是将其放入listings一个standalone文件中并导入编译结果。但是,结果看起来不太好。

答案2

已编辑以提供单独的列表环境(浮动)。

这是一个答案(类似于 A.Ellett 的输出,但使用verbatimbox包,而不是listings。它还将其放入侧边图形框中,这样它就不会跨越页面边界。

\documentclass[a4paper,twoside,11pt,openright]{scrbook}     
\usepackage[demo]{graphicx}           
\usepackage[wide]{sidecap}                                      
\usepackage[font=footnotesize,
    format=plain,
    labelfont={bf,sf},
    textfont={it},
    width=10pt]{caption}
\usepackage{verbatimbox}
\usepackage{xcolor}
\newcommand{\fig}[4]{
    \begin{SCfigure}
      \centering
            \includegraphics[width=\textwidth]{#1}
      \caption[#2]{#3}
      \label{fig:#4}
    \end{SCfigure}
}
\usepackage{newfloat}
\DeclareFloatingEnvironment[placement={!ht},name=Listing]{listing}
\captionsetup[listing]{labelfont=bf}
\makeatletter
\makeatother

\begin{document}
\fig{background_flickr.png}{Flickr geotagging functionality}{Flickr
geotagging functionality. By navigating and panning on a map, the user
can place an image or video and specify the desired level of spatial
granularity.}{background_flickr}

\def\optarg{\colorbox{white}{%
  \makebox[.3in][r]{\arabic{VerbboxLineNo}:\hspace{1ex}}}%
}
\begin{verbbox}[\optarg]
{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    },
    "phoneNumbers": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}
\end{verbbox}

\begin{SClisting}
\fboxsep=0pt\relax\colorbox{lightgray}{\theverbbox}%
\caption[JSON Example]{A basic JSON example}
\end{SClisting}

\end{document}

在此处输入图片描述

相关内容