Include line-numbers in my code block in Latex - latex

I'd like to include line-numbers into my code block using Latex. However, currently the line-numbers are outside the grey code block and I don't know how I can adjust this. These are the following settings I have made.
\usepackage{listings}
\usepackage{xcolor}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
keywordstyle=\color{magenta},
numberstyle=\small\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
frame=lines,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle}
The current result looks as followed.
As you can see, the line-numbers 1-8 are outside the grey box. I'd like to have the numbers inside the grey box.
Also, how can I change the font size of the code in the grey box? I'd like to have the fontsize bigger.
Thank you very much in advance!

You can extend the frame around the numbers using the framexleftmargin option. Adding framexleftmargin=5mm to the style you provided in your question should do the trick.
This is described on page 38 of the listings package document, and an example on the bottom of that page shows a listing that uses this option to include the line numbers within the box. The listing package documentation is here: https://texdoc.org/serve/listings.pdf/0.
Also, the font size is set by basicstyle=\ttfamily\footnotesize which is currently setting the font of the code to footnotesize, which is pretty small. You can change this to "normalsize" by simply removing that part of the instruction and leaving basicstyle=\ttfamily. You can increase the font size further by using \large, \Large, or even \huge.

Related

Latex page issues

\documentclass{article}
\usepackage{geometry}
\usepackage{amsthm, amsmath, amssymb}
\usepackage{anyfontsize}
\newgeometry{vmargin={15mm}, hmargin={12mm}}
\makeatletter
\newcommand{\mathleft}{\#fleqntrue\#mathmargin0pt}
\newcommand{\mathcenter}{\#fleqnfalse}
\makeatother
\begin{document}
\fontsize{18}{21.6}
\mathleft
\begin{gather*}
8x^2+10x-3=0\\\\
8x^2-2x+12x-3=0\\\\
2x(4x-1)+3(4x-1)=0\\\\
(2x+3)(4x-1)=0\\\\
x=-\frac{3}{2}\;or\;\frac{1}{4}\\\\\\\\\\\\
-7x^2-4x+3=0\\\\
-7x^2-7x+3x+3=0\\\\
-7x(x+1)+3(x+1)=0\\\\
(3-7x)(x+1)=0\\\\
x=\frac{3}{7}\;or\;-1\\\\\\\\\\\\
x^2+4=8x-8\\\\
x^2-8x+12=0\\\\
x^2-2x-6x+12=0\\\\
x(x-2)+-6(x-2)=0\\\\
(x-6)(x-2)=0\\\\
x=2\;or\;6
\end{gather*}
\end{document}
When I run this code I get a pdf with the first page blank and some of the actual text outside the page. How can I fix it so that it does not leave a blank page and the text stays in the page so that if it can't fit it creates a new page and puts the text there. Both of these issues disappear if the text between begin gather and end gather is small, but how do I make it work regardless of the amount of text or size of the font? The question is similar to this but I am not using the xcolor package or counters. To get the pdf I am using the TexShop application installed from mactex.

Koma scrbook seems not to include BCOR

The proof copies of a book laid out with KOMA scrbook show that the inner margins are too narrow. I had BCOR set to 10mm and want to increase them, but my experiments with BCOR set to 20mm show that the layout is the same independent what BCOR value I set. Here my MWE:
\documentclass{scrbook}
\KOMAoptions{paper=
128.5mm:198.4mm, %(5,06" x 7,91") %ziel
BCOR=20mm,
twoside,
headinclude=false, footinclude=false,
headings=normal,
titlepage=true,
% draft=true,
DIV=9, %ziel kleines buch
fontsize=12pt,
}
\usepackage[german]{babel}
\usepackage{lipsum}
\usepackage{geometry}
\usepackage[a4,axes,cross,pdftex,center]{crop}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\part{ Philosophie}
some text
\lipsum
\lipsum
\end{document}
What is wrong?
My real question is: how can I just move the text area on both pages to the outside (i.e. making the gutter wider) without changing the text area size (and thus preserve line breaks)?
I found a hint in some other questions answered before and the problem was caused by the interaction between the KOMA script and the geometry package. Using the geometry package recalculates the page layout and overwrites the one calculated by scrbook including the BCOR value. This can be avoided when adding the option pass to geometry as in
`\usepackage[pass]{geometry}`.
In general the interaction between the layout calculated by KOMAscript and other package can lead to surprises. The discussion in https://tex.stackexchange.com/questions/182821/div-and-bcor-setting-in-koma-best-practices/183839 is very useful.

Inconsistent styling in latex table of contents?

I'm wondering why in my toc shown below, I have different dots style in the most detailed subsections ?
All I've used is:
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
....
\tableofcontents
How can I create a consistent style at all levels of depth: section, subsection, etc?
It really depends on the document class you are using; book, article etc have a preset layout for the TOC.
Since you said you are writing your Thesis, is it possible that you are using a style given by the college/university?
There is still a way to change it. You can use the package tocloft,
\usepackage{tocloft}
which has a method called \#dotsep to change the amount of space between the dots. Although I do not know if it will work in this case as you only define it once for the whole TOC and if it is relative to what you have it may just increase your dots relative to the way it is already placed.
But here is the code anyways,
\makeatletter \renewcommand{\#dotsep}{4.5} \makeatother
\tableofcontents
It must be placed before your toc command and 4.5 represents the distance.
But like I said this depends on your document class and its style class. Here is the link to the package documentation tocloft.pdf. There are more customizations available which may work for you better.
At least in the article class, they already have a standard style, but to also get dot leaders for sections like the ones in subsections, just use this:
\usepackage{tocloft}
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}

latex template or example for personal statement

I am writing a personal statement in latex. I don't want the big margin at the top of the page not big title taking a lot of space. I just like to make the layout compact but still clearly spaced with title, name and other necessary information, since there may be restriction on the number of pages. One example would be http://www.hsc.unt.edu/education/CIM/Documents/PS-Sample2_000.pdf. I wonder where to find some good latex templates or examples?
Thanks and regards!
I would use the geometry package to establish the desired margins. To get the margins in your sample document, try:
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry}
Your next requirement was to fix the title block. LaTeX uses the internal command \#maketitle to format the title block. You can redefine this as you like. To achieve the same title block style as in the sample document, use:
\usepackage[svgnames]{xcolor}% provides colors for text
\makeatletter% since there's an at-sign (#) in the command name
\renewcommand{\#maketitle}{%
\begin{center}
\parskip\baselineskip% skip a line between paragraphs in the title block
\parindent=0pt% don't indent paragraphs in the title block
\textcolor{red}{\bf\#title}\par
\textbf{\#author}\par
%\#date% remove the percent sign at the beginning of this line if you want the date printed
\end{center}
}
\makeatother% resets the meaning of the at-sign (#)
The \#title, \#author, and \#date commands will print the title, author, and date. You can use whatever formatting commands you like to set the text in bold, different colors, etc.
Put all of the above commands in the preamble of the document. The preamble is the space between \documentclass and \begin{document}.
\documentclass{article}
% this is the preamble
% put all of the above code in here
\title{Personal Statement}
\author{Tim}
\begin{document}
\maketitle% prints the title block
Emergency medicine has always been a passion of mine\ldots
\end{document}
Attempt #1: I've used the following style file, which I call cramp2e, for similar purposes. It is probably not right for you, but have a look:
\oddsidemargin -1cm
\evensidemargin -2cm
\topmargin 1cm
\textheight 24cm
\textwidth 19cm
\headheight 0cm
\headsep .7cm
\footskip .7cm
\parskip .2cm
\paperheight 25cm
\setlength\voffset{-.33in}
\setlength\hoffset{-.25in}
Any good?
Postscript This is for A4 size paper.
A slightly less LaTeX-ey solution would be to not use the \maketitle command. A couple of times I've simply used this as my title(marginsize helps too).
Set up smaller margins:
\documentclass{article}
\usepackage{anysize}
\marginsize{1cm}{1cm}{1cm}{1cm}
(EDIT: 1cm might be even better..)
Minimal title:
\begin{document}
\begin{center}
\section*{My Document Title}
\today
\end{center}
% content goes here
\end{document}
The result looks something like:

How do I control the width of a \caption{} in a float? (LaTeX)

I am looking for a simple way to control the width of a \caption{} inside a float in LaTeX without using the {caption} package. Any clues?
(EDIT): The problem I am having is that the built-in captions are too narrow.
In general, to control the width of objects in TeX, you can use a minipage, for instance:
\begin{minipage}[t]{1.85in}
\caption{ Why would you eat a pickle? }
\end{minipage}
However, with a caption you might need to do something different as it could very well be a macro.
To change the formatting of the standard LaTeX classes without using a package, you need to look at the source of the standard LaTeX classes and alter the original definitions in your own document. Printing captions is done by the macro \#makecaption, which has definition (in article.cls):
\long\def\#makecaption#1#2{%
\vskip\abovecaptionskip
\sbox\#tempboxa{#1: #2}%
\ifdim \wd\#tempboxa >\hsize
#1: #2\par
\else
\global \#minipagefalse
\hb#xt#\hsize{\hfil\box\#tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}
If you wrap the whole thing in a minipage environment (as suggested to be done manually in the other answers), you should get the results you want.
Is it easier than loading a package? Not really, but it can be instructive.
There's nothing too magical about \caption; it just consults a figure/table number and than formats your text at a font and size that the document class likes. So you can control its width the same way you'd control the width of any text:
Wrap the \caption{...} in a \parbox or a \begin{minipage}...\end{minipage}.

Resources