Lyx and Latex notes

Latex is a document preparation system, commonly used for creating PDF files. Lyx is a document processor, which is basically a GUI for Latex, with enhancements. These notes have been tested with Lyx version 1.5.4.

Index

Resize editor text

To change the size of the text as you see in the editor (not in the document output):

  1. Tools -> Preferences…
  2. Look & Feel -> Screen Fonts
  3. Change Zoom %.

Install a LaTeX package

To manually install a package, including classes (.cls) or styles (.sty):

  1. Find out where to place the package by starting Lyx, going to Tools -> TeX Information and selecting ‘Show path’.
  2. Download the package and place in the appropriate directory (e.g. /usr/share/texmf-dist/tex/latex/).
  3. In Lyx go to \usepackage{packagename}

Apparently you can use a package manager instead, such as TeXLive or MikTeX, but I haven’t done this so can’t advise.

Format and colour code listings

In Lyx, go to Document -> Settings -> LaTeX Preamble:

\usepackage{listings,xcolor}
\definecolor{dkgray}{rgb}{.4,.4,.4}
\definecolor{dkred}{rgb}{.4,0,0}
\definecolor{dkgreen}{rgb}{0,.4,0}
\definecolor{dkblue}{rgb}{0,0,.4}

Insert -> Program Listing. Right click -> Settings -> Main Settings tab:

Language: PHP
Break long lines
Line numbering Side: Left

Advanced tab (box on right):

commentstyle={\color{dkgray}}
frame={topline,rightline,bottomline,leftline}
identifierstyle={\color{dkgreen}}
keywordstyle={\color{dkblue}}
stringstyle={\color{dkred}}

References

Remove page number

To disable page numbering for a particular page, insert the following TeX code:

\thispagestyle{empty}

References

Reduce title padding

To reduce the padding / spacing around the title and presumably the date or any other content that normally goes at the top of a document, use the TeX vskip command.

Insert -> TeX code:

\vskip -2em

This will reduce the spacing by 2em. You can use other units like cm, pt, etc.

Add this before and after the title - basically anywhere you’d like to reduce the vertical skip.

Reference: Reducing the title spacing

Footers

To get a footer on each page of your document, first change the page style to fancy (document -> settings -> page layout -> page style -> fancy). You can now set the footer in the document preamble (document -> latex preamble) by adding one of the following, for a footer aligned left, centre, or right:

\lfoot{Text for the left of the footer}
\cfoot{Text for the centre of the footer}
\rfoot{Text for the right of the footer}

If you attempt to view the document (as PDF or whatever) and you get the following error…

Undefined control sequence.
LaTeX Error: Missing \begin{document}.

…then it is likely that you forgot to change the page style to ‘fancy’ (i.e. Lyx doesn’t understand the footer command in the preamble).

Preamble for document metadata and styling

The Latex “preamble” can be used to set document metadata, like the title, author and date, and add styling, such as headers/footers.

To re-use metadata, create your own variables and reference these wherever you’d like, either in the preamble or in the document itself.

Example preamble:

\newcommand\mytitle{Mindspill}
\newcommand\myauthor{Stephan}
\newcommand\myurl{http://mindspill.net}
\newcommand\mydate{01/08/2008}
\title{\mytitle}
\author{\myauthor\space\myurl}
\date{\mydate}
\lhead{Section \thesection}
\chead{}
\rhead{Page \thepage}
\lfoot{\mytitle.\space\myauthor}
\cfoot{}
\rfoot{\mydate} 
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\renewcommand{\abstract}{\flushleft}

The example creates 4 variables, called mytitle, myauthor, myurl and mydate. These are reused in the title, author and date metadata declarations and in the footer.

In addition, the preamble sets the header/footer line rule and makes the abstract text left aligned.

Note the use of \space to get a space character.

The custom variables can be referenced elsewhere in your document using the same code as in the preamble. In Lyx, this would be done by inserting via Insert -> Tex Code.

BibTeX usage

Set the following preamble to wrap long urls.

\usepackage {url}
%% numbered citations with natbib
%% this is where you can specifiy all natbib's options
\usepackage [numbers]{natbib}

Create a .bib file. Add something to it.

%@article{gpl,
%    author = {Free Software Foundation},
%    title= {GNU General Public License},
%    url = {http://www.gnu.org/licenses/gpl.html},
%    note = {Version 2},
%    year = {1991}
%}
@article{Wikipedia,
    organisation = {Wikipedia},
    author = {Wikipedia},
    title = {Web application},
    abstract = {Description of the term 'Web application'},
    url = {http://en.wikipedia.org/wiki/Web_application},
    year = {2006},
    month = {12},
    day = {14},
    note = {Downloaded as en.wikipedia.org-wiki-Web_application.html}
}

Note: The first example, commented out, shows the entry types that are shown in the output. If the month was included, that would be displayed too. The Wikipedia example includes supporting entries that are good to include for completeness (you could later use a different style that shows these entries).

Add the bibliography to the end of your document. Insert -> List / TOC -> BibTeX Bibliography… Browse to your .bib file and add it. Select the plainnat style (I didn’t have natbib as an available style).

Create a citation somewhere in the document. Insert -> Citation. Add citations as necessary.

You add/remove items to/from your bibliography by editing the .bib file. You can edit the bibliography settings by clicking on it’s icon in the document. You can edit the citations by clicking on their icons in the document.

References

  • http://www.ecst.csuchico.edu/~jacobsd/bib/formats/bibtex.html
  • http://www.itee.uq.edu.au/~emmerik/lyxthesis.html
  • http://web.reed.edu/cis/Help/LaTeX/bibtexstyles.html

Gotchas

File luainputenc.sty not found

If you get the following error when generating a PDF (via File -> Export -> PDF (LuaTeX), or pdf preview)…

LaTeX Error: File `luainputenc.sty` not found.

…then you can disable luainputenc.

Apparently luainputenc is for compatibility with old documents, so it probably isn’t needed.

Change the document language to not use inputenc:

Document -> Settings -> Language -> Language Default (no inputenc).

References

Last modified: 23/04/2015 Tags: , ,

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top