0

I'm trying to add an environment for code listings to stay on one page as described in "How to prevent lstlisting from splitting code between pages?".

When my listing is at the bottom of the page, the page number will appear within the environment. How do I force the page number to display outside of the environment?

An example follows. In this case the environment takes up the entire page. If possible I would like to remove page numbers entirely when it would cause a page boundary to be reached.

\documentclass{scrreprt}
\usepackage{bera}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{xcolor}

\definecolor{background}{HTML}{EEEEEE}
\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}

\lstset{
  basicstyle=\normalfont\ttfamily,
  numbers=left,
  numberstyle=\scriptsize,
  stepnumber=1,
  numbersep=8pt,
  showstringspaces=false,
  breaklines=true,
  frame=lines,
  backgroundcolor=\color{background},
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily,
  moredelim=[il][\textcolor{pgrey}]{$$},
  moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}

\lstnewenvironment{code}[1][]%
{
  \noindent
  \minipage{\linewidth}
  \vspace{0.5\baselineskip}
  \lstset{basicstyle=\ttfamily\footnotesize,frame=single,#1}}
{\endminipage}

\begin{document}
\begin{figure}[H]
\begin{code}[language=Java]
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

public class SesSmtpCredentialGenerator {
       private static final String KEY_ENV_VARIABLE = "AWS_SECRET_ACCESS_KEY"; // Put your AWS secret access key in this environment variable.
       private static final String MESSAGE = "SendRawEmail"; // Used to generate the HMAC signature. Do not modify.
       private static final byte VERSION =  0x02; // Version number. Do not modify.

       public static void main(String[] args) {

              // Get the AWS secret access key from environment variable AWS_SECRET_ACCESS_KEY.
              String key = System.getenv(KEY_ENV_VARIABLE);
              if (key == null)
              {
                 System.out.println("Error: Cannot find environment variable AWS_SECRET_ACCESS_KEY.");
                 System.exit(0);
              }

              // Create an HMAC-SHA256 key from the raw bytes of the AWS secret access key.
              SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA256");

              try {
                     // Get an HMAC-SHA256 Mac instance and initialize it with the AWS secret access key.
                     Mac mac = Mac.getInstance("HmacSHA256");
                     mac.init(secretKey);

                     // Compute the HMAC signature on the input data bytes.
                     byte[] rawSignature = mac.doFinal(MESSAGE.getBytes());

                     // Prepend the version number to the signature.
                     byte[] rawSignatureWithVersion = new byte[rawSignature.length + 1];
                     byte[] versionArray = {VERSION};
                     System.arraycopy(versionArray, 0, rawSignatureWithVersion, 0, 1);
                     System.arraycopy(rawSignature, 0, rawSignatureWithVersion, 1, rawSignature.length);

                     // To get the final SMTP password, convert the HMAC signature to base 64.
                     String smtpPassword = DatatypeConverter.printBase64Binary(rawSignatureWithVersion);
                     System.out.println(smtpPassword);
              }
              catch (Exception ex) {
                     System.out.println("Error generating SMTP password: " + ex.getMessage());
              }
       }
}
\end{code}
\caption{Java implementation of SES password algorithm\label{fig:ses-password}}
\end{figure}
\end{document}
5
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    Oct 1, 2014 at 19:45
  • 2
    @ChristianHupfer when creating a minimal working example I discovered how to correct the initial issue I was seeing and realized what my actual question was. Oct 1, 2014 at 20:45
  • \thispagestyle{empty}
    – user11232
    Oct 1, 2014 at 23:41
  • @HarishKumar I know how to hide a page number on a particular page. Is there a way to detect if a page number would overlap an environment and disable it if that's the case? Oct 1, 2014 at 23:46
  • 1
    If you replaced the normal page number using the everypage package you could make the page number conditional. All you would need is a way to detect the actual bottom of the text is. Oct 2, 2014 at 0:55

0

You must log in to answer this question.

Browse other questions tagged .