Text FormattingSet up a Code Text Box

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
ougka
Posts: 43
Joined: Wed Jan 19, 2011 5:02 pm

Set up a Code Text Box

Post by ougka »

Hi all,

I am trying to get a text box in my latex document that includes C code or context from configuration files. What I want is something similar to the code tag in the forum. Essentially, I want a textbox that :
  1. Spans nicely in multiple lines
  2. Is framed and has flushed left text
  3. Its background colour is e.g. a light grey
  4. Its context is on a different font type than my paragraph's text
  5. I am able to modify margins around the text it contains
  6. I am able to reference it, e.g. as a figure or perhaps as a separate category (e.g. code)
I've tried to use the \framebox but it's too restrictive and doesn't span appropriately in multiple lines. Would a minipage be more helpful? I am guessing that I have to create a newcommand with e.g. \mytextbox but could someone help here?

thanks a lot!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Set up a Code Text Box

Post by localghost »

Search the forum for solutions by the packages listings or minted. This issue has been discussed very often. Feel free to ask further questions if you run into trouble with a basic structure.


Thorsten
ougka
Posts: 43
Joined: Wed Jan 19, 2011 5:02 pm

Re: Set up a Code Text Box

Post by ougka »

Hello,

Well, I couldn't find a good example of what I needed in the forum but it's all about experimentation (although, honestly, it's a bit nerve wrecking)

So I am using listings! How do I :

1) Name the List of Listings in my TOC as "List of Listings" and not just "Listings"?
2) Change the font of the box?
3) Add a bullet point symbol in the code! I used to do $\bullet$ but obviously it doesn't work in a \begin{lstlisting}
4) how do I make it to have text around it and not be in a page on its own? This happens if I use the option float, but if I don't use it the listing breaks between pages which is ugly..

Thanks a lot!
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Set up a Code Text Box

Post by localghost »

Since I don't have the time for a detailed solution, here are some hints that let you solve the problem.
ougka wrote:[…] 1) Name the List of Listings in my TOC as "List of Listings" and not just "Listings"? […]
According to Section 4.9 of the manual you have to redefine \lstlistlistingname.
ougka wrote:[…] 2) Change the font of the box? […]
Read Section 4.6 of the package manual to setup the basic style of your listings.
ougka wrote:[…] 3) Add a bullet point symbol in the code! I used to do $\bullet$ but obviously it doesn't work in a \begin{lstlisting} […]
Read Section 4.14 of the package manual for escaping to LaTeX.
ougka wrote:[…] 4) how do I make it to have text around it and not be in a page on its own? This happens if I use the option float, but if I don't use it the listing breaks between pages which is ugly. […]
This should not occur if you just assign a caption to your listing. The listing floats but still allows text on the same page. See Section 4.3 of the package manual.
ougka
Posts: 43
Joined: Wed Jan 19, 2011 5:02 pm

Set up a Code Text Box

Post by ougka »

Hello localghost,

Thanks for your reply. I hope that you come back because I haven't solved these issues...
localghost wrote:Since I don't have the time for a detailed solution, here are some hints that let you solve the problem.
ougka wrote:[…] 1) Name the List of Listings in my TOC as "List of Listings" and not just "Listings"? […]
According to Section 4.9 of the manual you have to redefine \lstlistlistingname.
Well, I tried to do this with the following :

Code: Select all

\renewcommand{\lstlistlistingname}{List of Listings}
but it didn't work.. any ideas? Please note that I want to chance the title of the Listings section in my TOC.
localghost wrote:
ougka wrote:[…] 2) Change the font of the box? […]
Read Section 4.6 of the package manual to setup the basic style of your listings.
Well, I read that but without any luck. I just want to change the font type, what is the style that they keep referring to in the manual?
localghost wrote:
ougka wrote:[…] 3) Add a bullet point symbol in the code! I used to do $\bullet$ but obviously it doesn't work in a \begin{lstlisting} […]
Read Section 4.14 of the package manual for escaping to LaTeX.
Done!
localghost wrote:
ougka wrote:[…] 4) how do I make it to have text around it and not be in a page on its own? This happens if I use the option float, but if I don't use it the listing breaks between pages which is ugly. […]
This should not occur if you just assign a caption to your listing. The listing floats but still allows text on the same page. See Section 4.3 of the package manual.
Well, if I don't use the word float, it breaks in two pages, and if use it, it goes on a page itself without any text around it (despite the fact that there is space around it for text)!

I would appreciate any help!
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Set up a Code Text Box

Post by localghost »

ougka wrote:[…] Well, I tried to do this with the following :

Code: Select all

\renewcommand{\lstlistlistingname}{List of Listings}
but it didn't work.. any ideas? Please note that I want to chance the title of the Listings section in my TOC. […]
How do you get that ToC entry?
ougka wrote:[…][…] Well, I read that but without any luck. I just want to change the font type, what is the style that they keep referring to in the manual? […]
I always wonder how one can read a manual "without any luck". Section 4.6 clearly points out how to use the basicstyle option.
ougka wrote:[…] Well, if I don't use the word float, it breaks in two pages, and if use it, it goes on a page itself without any text around it (despite the fact that there is space around it for text)! […]
You are right. But it depends on the size of the listing. I managed to get a floating listing along with text on one page.

I suggest that you submit a true minimal example that shows your recent efforts. Occurring problems will be much easier to solve then. Otherwise the discussion remains theoretic.
Post Reply