Make code in LaTeX look *nice* [closed] - latex

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm currently writing a project report, and to explain the slightly crazy un-intuitive code that I've used I need to put lots and lots of short excerpts of example code in.
How I can get code into LaTeX that:
looks nice
doesn't go off the side of the page if the line's too long (list lstlisting or verbatim)
is preferably inline with the rest of the text
EDITED (Figured I'd add the settings so people don't have to try and figure them out themselves (taken from wikibooks (link further down) and edited for niceness))
Nice settings:
\usepackage{color}
\usepackage{listings}
\lstset{ %
language=C++, % choose the language of the code
basicstyle=\footnotesize, % the size of the fonts that are used for the code
numbers=left, % where to put the line-numbers
numberstyle=\footnotesize, % the size of the fonts that are used for the line-numbers
stepnumber=1, % the step between two line-numbers. If it is 1 each line will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
tabsize=2, % sets default tabsize to 2 spaces
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
escapeinside={\%*}{*)} % if you want to add a comment within your code
}
\begin{lstlisting}
!!code!!
\end{lstlisting}
An example of these settings:

The listings package is quite nice and very flexible (e.g. different sizes for comments and code).

It turns out that lstlisting is able to format code nicely, but requires a lot of tweaking.
Wikibooks has a good example for the parameters you can tweak.

For simple document, I sometimes use verbatim, but listing is nice for big chunk of code.

Related

Wrap Sentences in Tabular Environment [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I'm using the tabular environment in Latex (Overleaf) and would like my text to wrap automatically and be justified. The closest I can get to this is to use the \\ syntax. This is laborious; further, I would like to have justified text, like the rest of my document. I am using a snippet that I found here, but it does not incorporate automatic wrapping and justified text. My minimum working example follows:
\documentclass[11pt,fleqn]{book}
\usepackage{import}
\usepackage{fancyhdr}
\fancyhf{}
\cfoot{\thepage}
\pagestyle{fancy}
\usepackage{soul}
\usepackage[utf8]{inputenc}
\usepackage{subfiles}
%\bibliographystyle{aea}
\usepackage[backend=biber,style=authoryear,sorting=nyvt]{biblatex}
\addbibresource{newbib3.bib}
\begin{document}
\noindent\textbf{Analysis of Editing}\\
\noindent{Substantive}: 0, -2.\\
Further: the, mighty.\\
\begin{tabular}{#{}l#{\ }l}
Value Added: & Yes, I want the benefit of using the tabular environment.\\
& No, because I would like this text to wrap automatically (instead of extending out continuously) and to be justified.
\end{tabular}
\end{document}
Which produces the following:
Analysis of Editing
Substantive: 0, -2.
Further: the, mighty.
Value Added: Yes, I want the benefit of using the tabular environment.
No, because I would like this text to wrap automatically (instead of extending out continuously) and to be justified.
You could either use a column of fixed width, e.g. p{5cm} or a tabularx that will automatically adjust the width of the X columns to fill the desired with.
Unrelated note: please do not abuse \\ for line breaks outside of tables
\documentclass[11pt,fleqn]{book}
\usepackage{import}
\usepackage{fancyhdr}
\fancyhf{}
\cfoot{\thepage}
\pagestyle{fancy}
\usepackage{soul}
\usepackage[utf8]{inputenc}
\usepackage{subfiles}
%\bibliographystyle{aea}
\usepackage[backend=biber,style=authoryear,sorting=nyvt]{biblatex}
\addbibresource{newbib3.bib}
\usepackage{tabularx}
\begin{document}
\noindent\textbf{Analysis of Editing}
\noindent{Substantive}: 0, -2.
Further: the, mighty.
\begin{tabularx}{.953\linewidth}{#{}l#{\ }X}
Value Added: & Yes, I want the benefit of using the tabular environment.\\
& No, because I would like this text to wrap automatically (instead of extending out continuously) and to be justified.
\end{tabularx}
\end{document}

Writing a CV in Latex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I am trying to write a CV in Latex, but now I am stuck. Since I want to learn Latex, I don't really want to just use a template.
I am thinking about one page divided in to columns/minipages:
Left column: an image + contact information etc.
Right Column: Education, experience, volunteer work etc.
Tried minipages and columns, but the image is destroying the whole format... Tried to use parts from templates, which worked fine, i.e. secsty etc. See example under.
Not a specific question, but hope someone has a good idea for how to make something out of this idea :)
Example:
%Preamble
\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{geometry}
\geometry{a4paper, total={170mm,257mm}, left=10mm, top=10mm}
\pagestyle{empty}
%%% Custom sectioning (sectsty package)
\usepackage{sectsty}
\sectionfont{\usefont{OT1}{phv}{m}{n}\sectionrule{0pt}{0pt}{-10pt}{1pt}}
\subsectionfont{\usefont{OT1}{phv}{m}{n}}
%Document
\begin{document}
\begin{minipage}[250mm]{0.4\linewidth}
\includegraphics{image.png}
Date of birth:
Phone:
Email:
\end{minipage}
\begin{minipage}[250mm]{0.65\linewidth}
\part*{Name}
\section*{EDUCATION}
\section*{EXPERIENCE}
\end{minipage}
\end{document}
It is great to see that you don't simply copy one of the existing templates, but want to learn some latex instead.
Some comments about your code:
if you want the minipages besides each other to emulate columns, their combined sum has to be equal or smaller than the linewidth. In your example 0.4+0.65= 1.05, so they won't fit in one line. In fact I would make them a bit smaller and add a \hfill between to get a bit of space between them
to make sure each minipage is only as wide as you specified, I would add the [width=\linewidth] option to the image. This will automatically scale the image correctly
if you want both minipages in one line, don't leave an empty line between them. In latex and empty line in the source code is interpreted as a paragraph break, thus forcing your second minipage in a new line
To get a better vertical alignment of the image, I would remove the optional height argument from the minipage and let tex determine how high your minipages are and add the options [T] and [b] respectively to determine the vertical alignment
this is more a design question: at the moment your page geometry is highly asymmetric. You can visualise this by adding showframe to your geometry options, e.g. \geometry{a4paper, total={170mm,257mm}, left=10mm, top=10mm,showframe}. Are you sure this is a good choice for a CV?
%Preamble
\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{geometry}
\geometry{a4paper, total={170mm,257mm}, left=10mm, top=10mm}
\pagestyle{empty}
%%% Custom sectioning (sectsty package)
\usepackage{sectsty}
\sectionfont{\usefont{OT1}{phv}{m}{n}\sectionrule{0pt}{0pt}{-10pt}{1pt}}
\subsectionfont{\usefont{OT1}{phv}{m}{n}}
%Document
\begin{document}
\noindent
\begin{minipage}[T]{0.4\linewidth}
\includegraphics[width=\linewidth]{example-image-duck}
Date of birth:
Phone:
Email:
\end{minipage}%
\hfill
\begin{minipage}[b]{0.55\linewidth}
\part*{Name}
\section*{EDUCATION}
\section*{EXPERIENCE}
\end{minipage}
\end{document}

Split caption of a figure into two equal lines [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to split for example the phrase:
Lower, upper and simulation throughput per user vs number of users
which appears in a caption of figure, into two lines in order for every line to have almost equal length. Something like the following:
Lower, upper and simulation
throughput per user vs number of users
How can I do that? I would appreciate any help!
You could wrap the contents in a varwidth environment (from the similarly-named varwidth package) of large-enough width. varwidth will shrink it to fit that natural content:
\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\begin{document}
\listoffigures
\begin{figure}
\centering
\includegraphics[scale=.5]{example-image-a}% Image
\caption[Lower, upper and simulation throughput per user \textit{vs.}\ number of users]
{\begin{varwidth}[t]{\linewidth}Lower, upper and simulation \\ throughput per user \textit{vs.}\ number of users\end{varwidth}}
\end{figure}
\end{document}
The reason for specifying the caption twice (once in the option argument [] and once as part of the mandatory argument {}) is because sending a box-constrained piece of text to the ToC doesn't look great. So, this separates the formatting between the two document components. Depending on the document class you're using, it is also possible to make this part of the regular label-setting requirements.
The caption package also provides format=hang with justification=raggedright that seems to duplicate your requirement. However, you'll have to play with the margins to adequately obtain the output you're after.

Stripes in background color drawn by listings package [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm trying to create a verbatim environment with a colored background and which can span across pages (so using a colorbox is not an option). It seemed that the listings package was a good way towards it, but the background is drawn one line at a time, such that, when I view the PDF, I see annoying white-ish "stripes" between the lines as well as where the invisible (0pt) frame rule was not to be drawn:
http://a.imageshack.us/img202/9928/lststrips.png
Here's the minimal code I used to create the output shown in the image:
\documentclass{minimal}
\usepackage[pdftex]{xcolor}
\usepackage[a4paper,hmargin=6cm]{geometry}
\usepackage{listings}
\lstset{backgroundcolor=\color{gray},
frame=single, framerule=0pt, framesep=5pt}
\begin{document}
\begin{lstlisting}
if (a < b)
{
printf("A is smaller than B!\n");
}
a = b;
\end{lstlisting}
\end{document}
Is there any workaround against these 'stripes'?
A simple workaround would be to not specify a color for the listings themselves, put instead use a \colorbox, but for that to work, you either need to use \lstinputlisting or store the result in a box using e.g. lrbox.
\newbox{\mybox}
\begin{lrbox}{\mybox}
\begin{minipage}{\linewidth}
\begin{lstlisting}
if (a < b)
{
printf("A is smaller than B!\n");
}
a = b;
\end{lstlisting}
\end{minipage}
\end{lrbox}
\colorbox{gray}{\usebox{\mybox}}
UPDATE: However, a more beautiful solution is to use Donald Arseneau's framed.sty, which also allows the source-code to span multiple pages.
\documentclass{minimal}
\usepackage[pdftex]{xcolor}
\usepackage[a4paper,hmargin=6cm]{geometry}
\usepackage{listings}
\usepackage{framed}
\begin{document}
\definecolor{shadecolor}{named}{gray}
\begin{shaded}
\begin{lstlisting}
if (a < b)
{
printf("A is smaller than B!\n");
}
a = b;
\end{lstlisting}
\end{shaded}
\end{document}

LaTeX - Adding a varying amount of horizontal spacing to the beginning of each new line in a sequence [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I'm using LaTeX for an algorithms assignment and I need to show the steps for Horspool's algorithm for string matching similar to what is shown in the textbook. The way it demonstrates the algorithm is showing how the pattern shifts along the text for each failed comparison, with each shift on a new line. The pattern is shown below the text with appropriate horizontal spacing indicating which letters are being compared.
Here is an example of what it would look like with a DNA sequence:
GAGTAATCCTTCACTTCAAGGCCAGTCTTCACATCTCATCAGA
ACATCTCA
ACATCTCA
ACATCTCA
ACATCTCA
I've poked around on several LaTeX references. I tried using \hspace to add the spacing at the beginning of each line, then adding \hfill after the pattern and before creating a newline. I'm not getting any errors, but there is no space being added at the front. The line is being filled correctly.
Is there another way to add the space to the beginning of each line, or another way to format this?
Just use \hspace*{5mm} (i.e. the starred form) which works fine at the beginning of a line.
I am fairly sure you want to typeset your examples with a fixed-width font, in which case, you should probably use verbatim environment. Even better, you could use fancyvrb package, which gives you a Verbatim environment, and allows you control over captioning, coloring, font size/shapes, etc. You can even make blanks more visible by showing them:
\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
\begin{Verbatim}[showspaces=true,fontsize=\small]
GAGTAATCCTTCACTTCAAGGCCAGTCTTCACATCTCATCAGA
ACATCTCA
ACATCTCA
ACATCTCA
ACATCTCA
\end{Verbatim}
\end{document}
For this, I always add an "empty" object to the beginning of the line, e.g.
${}$\hspace{5em}Foo
it's not beautiful, but serves the purpose.
I would simply use the listings package and do the indention with spaces or tabs.
I suppose that you are using the typewriter font anyway to get letters with a non-relative width.

Resources