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

Lemon
  • 3,153

2 Answers2

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.

Gonzalo Medina
  • 505,128
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}
Fran
  • 80,769