2

I am going to try to explain my situation.

I have a document that has a small font and prints in code. Unfortunately it only uses one side of the document, leaving a big white space on the other. I am trying to fill the other side up to save space.

Here is a sample

 \documentclass[10pt]{article}

 \textwidth = 520pt \textheight = 700pt \voffset = -90pt \oddsidemargin
 = -10pt \usepackage{listings} \usepackage{color}

 \usepackage{lmodern}

 \usepackage{scrextend} \changefontsizes[8pt]{8pt}

 \definecolor{dkgreen}{rgb}{0,0.6,0}
 \definecolor{gray}{rgb}{0.5,0.5,0.5}
 \definecolor{mauve}{rgb}{0.58,0,0.82}

 \lstset{frame=tb,   language=Java,   aboveskip=0mm,   belowskip=0mm,  
 showstringspaces=false,   columns=flexible,  
 basicstyle={\small\ttfamily},   numbers=none,  
 numberstyle=\tiny\color{gray},   keywordstyle=\color{blue},  
 commentstyle=\color{dkgreen},   stringstyle=\color{mauve},  
 breaklines=true,   breakatwhitespace=true   tabsize=90 }

 \begin{document}

 \begin{lstlisting}

 public void selectionSort(int[] numbers) {         
int min, temp;      
for (int x = 0; x < numbers.length; x++) {          
min = x;            
for (int y = x; y < numbers.length; y++) {

if (numbers[y] > numbers[min]) {
    min = y;}           
}           
temp = numbers[x];          
numbers[x] = numbers[min];          
numbers[min] = temp;        
}   }

 \end{lstlisting}
  \end{document}

If you compile, you will find that the entire RHS is white space. I want to fill that space with other text. Below is a quick screenshot of what is going on

enter image description here

6
  • I get a very short but quite fat document. There's lots of space beneath but none on the right. I've made your code into code but you need to remove the > marks so that people can read and use it easily.
    – cfr
    Apr 14, 2014 at 2:22
  • What do you mean with "the entire RHS"? I don't understand your problem. Could you please elaborate? Apr 14, 2014 at 2:26
  • I meant that if you compile, you would see a insertionSort() method on the left, but I made the font small so everything on the right has white space.
    – Lemon
    Apr 14, 2014 at 2:30
  • See screenshot for details.
    – Lemon
    Apr 14, 2014 at 2:33
  • I've provided an answer below, but I am not sure of this is what you had in mind. Please let me know of your intent was different. Apr 14, 2014 at 3:11

2 Answers 2

1

You can use two minipages:

\documentclass[10pt]{article}

 \textwidth = 520pt \textheight = 700pt \voffset = -90pt \oddsidemargin
 = -10pt \usepackage{listings} \usepackage{color}

 \usepackage{lmodern}

 \usepackage{scrextend} 

 \changefontsizes[8pt]{8pt}

 \definecolor{dkgreen}{rgb}{0,0.6,0}
 \definecolor{gray}{rgb}{0.5,0.5,0.5}
 \definecolor{mauve}{rgb}{0.58,0,0.82}

 \lstset{frame=tb,   language=Java,   aboveskip=0mm,   belowskip=0mm,  
 showstringspaces=false,   columns=flexible,  
 basicstyle={\small\ttfamily},   numbers=none,  
 numberstyle=\tiny\color{gray},   keywordstyle=\color{blue},  
 commentstyle=\color{dkgreen},   stringstyle=\color{mauve},  
 breaklines=true,   breakatwhitespace=true   tabsize=90 }

 \begin{document}

\noindent\begin{minipage}[t]{0.48\linewidth}
\begin{lstlisting}[linewidth=\linewidth]

public void selectionSort(int[] numbers) {         
int min, temp;      
for (int x = 0; x < numbers.length; x++) {          
min = x;            
for (int y = x; y < numbers.length; y++) {

if (numbers[y] > numbers[min]) {
    min = y;}           
}           
temp = numbers[x];          
numbers[x] = numbers[min];          
numbers[min] = temp;        
}   }

\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{0.48\linewidth}
\begin{lstlisting}[linewidth=\linewidth]

public void selectionSort(int[] numbers) {         
int min, temp;      
for (int x = 0; x < numbers.length; x++) {          
min = x;            
for (int y = x; y < numbers.length; y++) {

if (numbers[y] > numbers[min]) {
    min = y;}           
}           
temp = numbers[x];          
numbers[x] = numbers[min];          
numbers[min] = temp;        
}   }

\end{lstlisting}
\end{minipage} 

\end{document}

enter image description here

The vertical alignment can be controlled using the optional argument for minipage; in my example I used t for alignment at the top, but you could use b for bottom alignment or c for centered vertical alignment instead.

4
  • I was trying to also write code beside it. It kind of did what I was expecting, except that longer code gets cut off.
    – Lemon
    Apr 14, 2014 at 3:22
  • @sidht so, you want to write two listings side-by-side? Apr 14, 2014 at 3:24
  • yes, that is what I want, and possibly starting the code at the very top. Manipulating the page size doesn't seem to have a large effect after a certain value.
    – Lemon
    Apr 14, 2014 at 3:25
  • @sidht please see my updated answer. Apr 14, 2014 at 3:29
0

If there are not another text that must appear in one column in the same page, a simpler alternative to the minipages is the twocolumn option in the document class (see MWE). You can jump to the second column and from here to the next page with \newpage or \pagebreak.

If you need some text in one column in different pages, simply insert \onecolumn and \twocolumn when needed. Both commands force a new page. You can use these commands also in a document class without the twocolumn option.

If you need some text in one column at the top of the same page, use \twocolumn[text in one column].

If you one column text before and after the lists of code, or more than two columns, then see the package multicol.

\documentclass[twocolumn,10pt]{article}

 \textwidth = 520pt \textheight = 700pt \voffset = -90pt \oddsidemargin
 = -10pt \usepackage{listings} \usepackage{color}

 \usepackage{lmodern}

 \usepackage{scrextend} \changefontsizes[8pt]{8pt}

 \definecolor{dkgreen}{rgb}{0,0.6,0}
 \definecolor{gray}{rgb}{0.5,0.5,0.5}
 \definecolor{mauve}{rgb}{0.58,0,0.82}

 \lstset{frame=tb,   language=Java,   aboveskip=0mm,   belowskip=0mm,  
 showstringspaces=false,   columns=flexible,  
 basicstyle={\small\ttfamily},   numbers=none,  
 numberstyle=\tiny\color{gray},   keywordstyle=\color{blue},  
 commentstyle=\color{dkgreen},   stringstyle=\color{mauve},  
 breaklines=true,   breakatwhitespace=true   tabsize=90 }

 \begin{document}

 \begin{lstlisting}

 public void selectionSort(int[] numbers) {         
int min, temp;      
for (int x = 0; x < numbers.length; x++) {          
min = x;            
for (int y = x; y < numbers.length; y++) {

if (numbers[y] > numbers[min]) {
    min = y;}           
}           
temp = numbers[x];          
numbers[x] = numbers[min];          
numbers[min] = temp;        
}   }

 \end{lstlisting}

\pagebreak
 \begin{lstlisting}

 public void selectionSort(int[] numbers) {         
int min, temp;      
for (int x = 0; x < numbers.length; x++) {          
min = x;            
for (int y = x; y < numbers.length; y++) {

if (numbers[y] > numbers[min]) {
    min = y;}           
}           
temp = numbers[x];          
numbers[x] = numbers[min];          
numbers[min] = temp;        
}   }

 \end{lstlisting}


 \end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .