LyXProgramming Code in LYX

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
Post Reply
philouuuu
Posts: 3
Joined: Thu May 07, 2009 12:49 pm

Programming Code in LYX

Post by philouuuu »

Hello

I'm trying to write my thesis with LYX
I have to put many example of programming code but they aren't algorithm and I really dont know how to do.
If i use a tab, it put only one line and my code is 20 lines...

If you tell me to use a TEX box, put also the code that i have to use plz

:)

Recommended reading 2024:

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

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

fatra2
Posts: 126
Joined: Fri May 01, 2009 1:43 pm

Programming Code in LYX

Post by fatra2 »

Hi there,

If you are trying to point out the results of your programs, but adding the code into your tex document, I would definitely suggest something like a box environment. You could do something like

Code: Select all

\parbox[position]{width}{your text in paragraph form goes here.}
where "position" is not required in this case. If you want a frame around this box, to draw a little more attention to this part of the text you could do something like

Code: Select all

\fbox{par{width}{your text goes here in paragraph form.}}
Hope this helps. Cheers
philouuuu
Posts: 3
Joined: Thu May 07, 2009 12:49 pm

Re: Programming Code in LYX

Post by philouuuu »

I would like a border around my code

but are you sure about your second command?
it doesnt really work

and if i put
\fbox{
my code
with tab
and tab}

i won't take my double space, tab... everything will be in one line :(
fatra2
Posts: 126
Joined: Fri May 01, 2009 1:43 pm

Programming Code in LYX

Post by fatra2 »

Hi there,

I am sorry for my last post. I made a typo, and only noticed after I posted.

Like I said, there are few different options. You could also try

Code: Select all

\mbox{text}
which puts "text" in a box without border. Otherwise,

Code: Select all

\fbox{text}
which is identical to mbox, but frames the box. The two choices given above are very robust in LaTeX. Otherwise, you could also put your code into a minipage environment

Code: Select all

\begin{minipage}[position]{width}
with your code in here
\end{minipage}
After reading a bit more on this topic, I would recommend the minipage environment for which you can nest many different type of environment. If you want to have borders around your minipage, you can do

Code: Select all

\fbox{\beginminipage}[position]{width}
with your code in here
\end{minipage}}
Hope this helps. Cheers
philouuuu
Posts: 3
Joined: Thu May 07, 2009 12:49 pm

Re: Programming Code in LYX

Post by philouuuu »

fbox is perfect for the border
but minipage doesn't take car of the tabulation that i need in my programming code.
without tabulation, it "unreadable".

what must i use with the fbox to enable the tab?
safl
Posts: 3
Joined: Sat May 16, 2009 5:31 pm

Re: Programming Code in LYX

Post by safl »

I study computer science so i often have the exact same need.

What i do is i add this to the Preamble of the document (Document->Setting->Latex Preamble):

\usepackage{listings}
\lstset{language=c,numbers=left,tabsize=2,showstringspaces=false,showspaces=false,breaklines=true}

Then i insert (Insert->TeX Code) the following:

\inputlisting{code.cpp}

Where i want my code. It works for any language, I've used it with C/C++/Java/SML/Python.

This also let's your code live in the actual sourcefiles instead of copy-pasting them into the lyx document.
hyperwired
Posts: 1
Joined: Sat May 23, 2009 4:58 pm

Programming Code in LYX

Post by hyperwired »

safl wrote: ...
Then i insert (Insert->TeX Code) the following:
\inputlisting{code.cpp}
...
should be \lstinputlisting{filename.ext}

see also :
http://www.ctan.org/tex-archive/help/Ca ... tings.html
http://www.ctan.org/tex-archive/macros/ ... stings.pdf
Post Reply