2

Given this, how can I add a border around?

\begin{ttfamily}
\begin{lstlisting}
g@g0001:~$ cd Desktop
g@g0001:~/Desktop$ cat script.sh

#!/bin/bash
echo $1 `date`

g@g0001:~/Desktop$ ./script.sh "hello"
hello Wed Dec 19 06:30:49 CET 2012

g@g0001:~/Desktop$ ./script.sh "`ls -la`"
total 244 drwxr-xr-x 2 g g 4096 2012-12-19 06:29
. drwxr-xr-x 34 g g 4096 2012-12-19 06:23
.. -rwxr-xr-x 1 g g 314 2012-11-24 12:06
25332.txt -rw------- 1 g g 1038 2008-01-15 18:59
Makefile -rw-r--r-- 1 g g 227981 2011-09-06 10:37
.pdf -rwxrwxrwx 1 g g 29 2012-12-18 18:16
script.sh Wed Dec 19 06:31:18 CET 2012
\end{ttfamily}

I tried \fbox but I get an error.

  • 5
    Since you have some responses for your other questions of which some have valid answers, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Werner Dec 29 '12 at 23:23
  • Sorry about that. Once I tried to upvote but I got a message saying I couldn't just do that since my score was too low.. – g9999 Dec 29 '12 at 23:26
  • 1
    @g9999 Upvoting is different from accepting. You can't upvote answers or questions until your reputation is less than 15 (it's a mild protection against abuses); accepting will help other users with similar problems in understanding what answer was the most useful to the questioner. – egreg Dec 29 '12 at 23:34
  • 1
    @g9999 BTW, you can already upvote answers now that your reputation is greater than 15 :) – hpesoj626 Dec 30 '12 at 1:06
  • I have a solution but it doesnt't work properly. I added the following code: \lstset{framexleftmargin=1mm, frame=shadowbox, rulesepcolor=\color{black}} but unfortunately I always get an error. On the other end if I remove rulesepcolor=\color{black}, it works fine but of course I can't have a black frame. Thanks – g9999 Dec 30 '12 at 9:48
9

Are you trying to achieve something like this?

% http://tex.stackexchange.com/questions/88564/how-can-i-add-borders
\documentclass[border=5,preview]{standalone}

\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
frame=single
}
\begin{document}
\begin{lstlisting}
g@g0001:~$ cd Desktop
g@g0001:~/Desktop$ cat script.sh

#!/bin/bash
echo $1 `date`

g@g0001:~/Desktop$ ./script.sh "hello"
hello Wed Dec 19 06:30:49 CET 2012

g@g0001:~/Desktop$ ./script.sh "`ls -la`"
total 244 drwxr-xr-x 2 g g 4096 2012-12-19 06:29
. drwxr-xr-x 34 g g 4096 2012-12-19 06:23
.. -rwxr-xr-x 1 g g 314 2012-11-24 12:06
25332.txt -rw------- 1 g g 1038 2008-01-15 18:59
Makefile -rw-r--r-- 1 g g 227981 2011-09-06 10:37
.pdf -rwxrwxrwx 1 g g 29 2012-12-18 18:16
script.sh Wed Dec 19 06:31:18 CET 2012
\end{lstlisting}
\end{document}

enter image description here

Here, the frame was achieved with the frame option with value single. The default value is none. You can choose from other values: leftline, topline, bottomline, lines, or shadowbox. Also, you don't need to put your listings inside \begin{ttfamily}...\end{ttfamily} as this can be set by the option basicstyle. The font styles can be achieved through options among others basicstyle, keywordstyle, numberstyle, identifierstyle, commentstyle, etc. You can set these and more.

Consult the listings manual (specifically Section 4.6 and 4.11) by clicking on this link or by entering texdoc listings in your terminal. For a full set of frame options, see Section 4.11.

1

You can set the frame for your listing:

\begin{lstlisting}[frame=single]
...
\end{lstlisting}

Or if you don't want to add frame=single to every lstlisting environment, you can define and set a default style:

\lstdefinestyle{mystyle}{frame=single}    
\lstset{style=mystyle}

For different border types, look at the Frames section of the documentation.

1

listings option frame is probably the easiest way to frame code, but there also exist other packages which provide more flexible and sophisticated environments for this purpose. tcolorbox is one of them. It includes an specific libraries for code listings processed with listings or minted.

Following code shows the default result and another one with some customization.

\documentclass[multi=tcblisting, border=2mm]{standalone}
\usepackage[most]{tcolorbox}
%\usepackage{listings}

\lstset{
basicstyle=\ttfamily,
}

\begin{document}
\begin{tcblisting}{listing only}
g@g0001:~$ cd Desktop
g@g0001:~/Desktop$ cat script.sh

#!/bin/bash
echo $1 `date`

g@g0001:~/Desktop$ ./script.sh "hello"
hello Wed Dec 19 06:30:49 CET 2012

g@g0001:~/Desktop$ ./script.sh "`ls -la`"
total 244 drwxr-xr-x 2 g g 4096 2012-12-19 06:29
. drwxr-xr-x 34 g g 4096 2012-12-19 06:23
.. -rwxr-xr-x 1 g g 314 2012-11-24 12:06
25332.txt -rw------- 1 g g 1038 2008-01-15 18:59
Makefile -rw-r--r-- 1 g g 227981 2011-09-06 10:37
.pdf -rwxrwxrwx 1 g g 29 2012-12-18 18:16
script.sh Wed Dec 19 06:31:18 CET 2012
\end{tcblisting}

\begin{tcblisting}{listing only, title=Framed listing with title, colback=blue!20, colframe=blue!70!black, sharp corners}
g@g0001:~$ cd Desktop
g@g0001:~/Desktop$ cat script.sh

#!/bin/bash
echo $1 `date`

g@g0001:~/Desktop$ ./script.sh "hello"
hello Wed Dec 19 06:30:49 CET 2012

g@g0001:~/Desktop$ ./script.sh "`ls -la`"
total 244 drwxr-xr-x 2 g g 4096 2012-12-19 06:29
. drwxr-xr-x 34 g g 4096 2012-12-19 06:23
.. -rwxr-xr-x 1 g g 314 2012-11-24 12:06
25332.txt -rw------- 1 g g 1038 2008-01-15 18:59
Makefile -rw-r--r-- 1 g g 227981 2011-09-06 10:37
.pdf -rwxrwxrwx 1 g g 29 2012-12-18 18:16
script.sh Wed Dec 19 06:31:18 CET 2012
\end{tcblisting}
\end{document}

enter image description here

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.