1

I want to know how to put a legend below the frame of a listings environment, such that it do not break the settings for the other two kind of lists that i have. Legend must be without number, aligned with the left margin of the frame, fixedname is "Fonte:", space below the frame equal to 10pt, space below legend till the text equal to 18pt, just like i have with tables, for example:

enter image description here

I know that i can control the distance between the legend and the frame with belowskip=xpt, in listings settings.

I'm using abntex2 class. And with the following settings I'm having the undesired result:

enter image description here

\renewcommand{\baselinestretch}{1.5}
\setlength{\beforechapskip}{-\onelineskip}
\setlength{\afterchapskip}{\onelineskip} 

\renewcommand{\foottextfont}{\small}              % default fontsize for document = 12pt                    
\renewcommand{\IBGEtabfontsize}{\small}
\renewcommand{\ABNTEXfontereduzida}{\foottextfont}
\captionsetup{justification=centering,labelfont=small,textfont=small,skip=4pt}

  \hangcaption
  \captiondelim{~\textendash~}
 %\captionnamefont{\ABNTEXfontereduzida}
 %\captiontitlefont{\ABNTEXfontereduzida}

\newcommand{\IBGEtabfontsize}{\ABNTEXfontereduzida}
\AtBeginDocument{%
   \newcommand{\configurecaptions}{\captionstyle[\raggedright]{}}
   \newcommand{\configureseparator}{\captiondelim{: }}
   \newsavebox{\myptabbox}
   \newlength{\myptabboxwidth}
   \newcommand{\IBGEtab}[3]{%
   \savebox{\myptabbox}{{\IBGEtabfontsize #2}}%
   \settowidth{\myptabboxwidth}{\usebox{\myptabbox}}%
   \centering%
   \parbox{\myptabboxwidth}{%
   \configurecaptions
     #1%
     \par%
   {\IBGEtabfontsize%
     #2%
      }%
    \par%
     #3}%
          }
\let\ibgetab\IBGEtab
}

\makeatletter
   \newcommand{\fonte}[2][\fontename]{%
     \M@gettitle{#2}%
     \memlegendinfo{#2}%
     \par
     \begingroup
        \@parboxrestore
          \if@minipage
          \@setminipage
          \fi
          \ABNTEXfontereduzida
          \configureseparator
         \@makecaption{\ABNTEXfontereduzida #1}{\ignorespaces\ABNTEXfontereduzida #2}\par
      \endgroup}
        \makeatother

\makeatletter
        \newcommand{\nota}[2][\notaname]{\fonte[#1]{#2}}
\makeatother

    \AtEndPackage{caption}{
      \AtBeginDocument{%
        \DeclareCaptionLabelSeparator{barr}{~\textendash~}
        \DeclareCaptionFont{ABNTEXfontereduzida}{\ABNTEXfontereduzida}
          \captionsetup{format=hang,
                        labelseparator=barr,
                        %labelfont=ABNTEXfontereduzida,
                        %font=ABNTEXfontereduzida
                        }
       \renewcommand{\configurecaptions}{%
                     \captionsetup{justification=justified,%
                     singlelinecheck=false}}
       \renewcommand{\configureseparator}{\captionsetup{labelsep=colon}}
      }
    }


% For list of tables and figures********************

 \newcommand{\Caption}[1]{ %
 \begin{Spacing}{1.5} %
        \caption{#1} %
        \end{Spacing} %
}

\newcommand{\Fonte}[2][\fontename]{ %
% \vspace{-3pt}
 \vspace{10pt}
  \fonte[#1]{#2}
  \vspace{-5pt}
}

% Notes below legend for tables and figures*********************
\newcommand{\Nota}[2][\notaname]{\fonte[#1]{#2}}

\usepackage{listings}   % New list of Listings******************

\renewcommand{\lstlistingname}{Código}                                 
\renewcommand{\lstlistlistingname}{Lista de Códigos}
\newfloat[chapter]{codigo}{loc}{\lstlistingname}                           % lol  (??)
\newlistof{listofcodigos}{loc}{\lstlistlistingname}
\newlistentry{codigo}{loc}{0}

\counterwithout{codigo}{chapter} % ABNT
\renewcommand{\cftcodigoname}{\lstlistingname\space}
\renewcommand*{\cftcodigoaftersnum}{\hfill--\hfill}

% Print new list of listings (Lista de Códigos)
\newcommand{\imprimirlistadecodigos}{ %
        \pdfbookmark[0]{\lstlistlistingname}{loc}
        \begin{KeepFromToc}                      
            \listofcodigos
        \end{KeepFromToc}
    \cleardoublepage %
}

% Listings settings******************************

    \lstset{
   %float=tp,                                                          
    extendedchars=false,
    inputencoding=utf8,
    language=[LaTeX]{TeX},
   %linewidth=13cm,
    backgroundcolor=\color{SkyBlue},
    keywordstyle=\color{black},
    rulecolor=\color{black},                                     
    commentstyle=\color{black}\ttfamily,
    basicstyle=\footnotesize\normalfont\ttfamily,                
    numberstyle=\small,                                          
    frame=single,                                                
    framesep=2pt,
   %rulesep=2pt,                                                
    xleftmargin=1.5cm,                                          
    xrightmargin=1.5cm,                                         
   %framexleftmargin=2pt,                                       
   %framexrightmargin=2pt,
   %framextopmargin=,
   %framexbottommargin=
    stepnumber=1,                                               
    numbers=left,                                               
    numbersep=8pt,                                              
    tabsize=2,                                                  
    upquote=true,
    columns=[c]flexible,                                        
    showstringspaces=false,
    breaklines=true,
   %frameround=ftff,                                            
    belowskip=0pt,
    belowcaptionskip=18pt,
   %numberbychapter=false,                                      
    breaklines=true,                                            
    upquote=false                                               
}

Test:

\documentclass[12pt,a4paper,openright,oneside,dvipsnames,english,brazil]{abntex2}
\usepackage{caption}                                                                  
\usepackage{tocloft}                                                                  
\usepackage{etoolbox}                                                               
\usepackage{lastpage}                                                               
\usepackage{graphicx}                                                               
\usepackage{calc}
\usepackage[prologue]{xcolor}

\begin{document}
\begin{codigo}[h!]
   \Caption{Código do Exemplo 1}\label{cod:cod1}
      \begin{lstlisting}
        \begin{document}
               Teste
         \end{document}
      \end{lstlisting}%
\Fonte{Elaborado pelo autor}
\end{codigo}
\end{document}   
  • Whatever it is, just put it into a \savebox, use \settowidth to determine how wide it is, then put whatever is to appear below the centered \usebox into with a \makebox. \parbox or minipage with the same width. BTW, your MWE is neither minimal nor working. – John Kormylo Feb 27 '15 at 3:51
  • John, to be honest, i don't know exactly what i did. But i've got the desired result for tables and figures. I put all that stuff above because i thought that could make things easier, and because i have a very short time to fix this. I will try to implement your suggestion. – Ulysses Feb 27 '15 at 4:25
1

Thank you John. I just have a very ugly solution.

\newcommand{\fontcod}{%
\hfill\\
\begin{minipage}[c]{13.2cm}
Fonte: Just me.
\end{minipage}\\
}

\lstset{
    float=tb,                                          
    extendedchars=false,
    inputencoding=utf8,
    language=[LaTeX]{TeX},
    linewidth=13cm,
    backgroundcolor=\color{SkyBlue},
    keywordstyle=\color{black},
    rulecolor=\color{black},                           
    basicstyle=\footnotesize\normalfont\ttfamily,      
    numberstyle=\small,                                
    frame=single,                                      
    framesep=2pt,
    xleftmargin=0cm,                                      
    xrightmargin=0cm,                                     
    stepnumber=1,                                    
    numbers=left,                                    
    numbersep=8pt,                                   
    tabsize=2,                                       
    upquote=true,
    columns=[c]flexible,                             
    showstringspaces=false,
    breaklines=true,
    belowskip=0pt,
    breaklines=true,                                                
    upquote=false                                          
}

\begin{document}

\begin{codigo}[h]
\Caption{\label{cod1}Test}
\centering
   \begin{tabular}{c}
      \begin{minipage}[t]{13cm}
          \begin{lstlisting}
                A very ugly solution.
          \end{lstlisting}
      \end{minipage}\\
   \fontcod
   \end{tabular}
\end{codigo}

\end{document}
  • Actually, this is simpler. Tabular does the same thing as I said automatically. – John Kormylo Feb 27 '15 at 17:22
  • Well, except you said you wanted the \fontcod left justified, not centered. You could do that using (p[13cm]} instead of {c}. – John Kormylo Feb 27 '15 at 17:37
0

Edit: switch \begin{minipage}[l]{13.2cm} for \begin{minipage}[c]{13.2cm} in \fontcod.

But print result is left justified with the code above (even with the wrong parameter edited):

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.