How to force line wrapping in listings package? - latex

I have a problem concerning the listings package in latex. I need to embed a source code of the following XML document http://www.sparxsystems.com.au/downloads/profiles/EP_Extensions.xml in my master thesis' appendix. The problem is, no matter what options I pass to the package, I don't seem to be able to fit it on the page. The lines in the listing are too long, and the listings package doesn't wrap them, which seems odd... What combination of options will do the trick?
At the moment I use the following lstset instruction:
\lstset{
breakindent=0em,
language=XML,
basicstyle=\footnotesize,
numbers=left,
numberstyle=\footnotesize,
stepnumber=2,
numbersep=5pt,
backgroundcolor=\color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
tabsize=2,
captionpos=b,
breaklines=true,
breakatwhitespace=true,
breakautoindent=true,
escapeinside={\%*}{*)},
linewidth=\textwidth
}
Which, with no other options set, and combined with the following command:
\lstinputlisting{EP_Extensions.xml}
Lists the given file's source to the following output:

I just had this problem...
breaklines worked for me, though you do need to set it to true - the default is false:
\lstinputlisting[style=Python,
caption=My Class,
label={mine.py},
breaklines=true,
]{../python/mine.py}
is pretty much what I used

I had this problem and this page ranks highly on google so here was the solution for me:
I had the following inclusion: \usepackage[none]{hyphenat} which causes the issue described above. I was using it to fix the problem of latex automatically breaking lines in the middle of a word, which looks really stupid in some circumstances.
After much searching for an alternative to this solution I found nothing suitable and settled for properly broken lines but badly placed hyphenation.

Not a great answer, but one idea is to word wrap the source code before including it. If you're on linux, the command fold can be used for this. Clearly this works best if the text is in a fixed width font, which i would recommend in any case for code listings.

A followup to Aaron's answer. I was using basicstyle=\ttfamily for my listings. I was therefore able to permit line breaking by changing
\usepackage[none]{hyphenat}
to
\usepackage[htt]{hyphenat}
% ^^^
The hyphenat docs describe that the htt option "enables hyphenation of ... text typeset via either \texttt or \ttfamily." In my use case, I didn't see any hyphenation in my listings, but I did get the breaking.

Related

Styling of biblography - change underline to italic (Latex)

I am struggeling with latex biblography styling.
Using the predefined
\bibliographystyle{plain}
style i get a nice result:
When adding the following package:
\usepackage {ulem}
I get this result in the biblography:
How can i remove the underline of the journal and get it italic?
I tried:
\renewcommand{\uline}[1]{\textit{#1}}
and also following, to undefine \uline
\renewcommand\uline[1]{}
But both create a lot of "Missing } inserted.\end{thebibliography}" in the bbl file. Is there a way to revert the \usepackage command or get back the old style?
One of the features of the ulem package is to redefine \emph so that it uses \uline, so (as is presumably already clear to you) that's why you're getting underline in the bibliography, where text was emphasised before.
The documentation for the ulem package notes that adding the [normalem] option when loading the ulem package means that \uline is defined but \emph isn't redefined. I don't know if that would be an acceptable solution for you – ie, do you in fact want \emph to be underlined throughout the rest of the document?
Alternatively, if you only want \emph to go back to its original definition for the bibliography, you could try (ulem docs again) giving the command \normalem before loading the bibliography. However, the way that the bibliography is loaded can sometimes make it unexpectedly difficult to fiddle with its formatting (as you've discovered, when trying to redefine \uline).
If these thoughts don't resolve things, try posting a ‘minimal working example’. You may also be better off looking at, or possibly posting to, the TeX stackexchange.
If anyone faced the issue of getting \hbuf errors in the Bibliography.
The Solution by NormanGrey helps in this case also:
The documentation for the ulem package notes that adding the [normalem] option when loading the ulem package...
Thus, just by making a small change in the settings.tex fixed this error:
\usepackage[normalem]{ulem}
It not only removed the underline, it fixed the overflow I had, as you can see here:
to
Thank you a lot.

no automatically word wrap with bibliography

I have a problem with my biblatex. My booktitles don't automaticly break lines in my bibliography. Has someone got a hint how to solve this? Is it possible to get an automatic break or do I have to set them manually? And if I so, how do I do that?
Here my biblatex code snippets:
\usepackage[backend=biber,style=authoryear,sorting=nyt,citestyle=authoryear]{biblatex}
\printbibliography[type=book,title={Books}]
See this example at IEEE: after Metrics is a linebreak is missing.
EDIT: I found the solution here
I was curious about the \emph{} style (mine was always underlining). I put a single \normalem in front of the \printbibliography command and it works fine :)
\normalem
\printbibliography[type=book,title={Books}]
Your book titles are underlined, which is preventing line breaks (underlined text doesn't break). I don't think this is a standard behavior, book titles are usually displayed in italics. Perhaps you are using \underline{} inside your bibliography items definitions?

sphinx customization of latexpdf output?

Just curious if anyone knows how to customize the sphinx output when using the latexpdf target? I've seen lots of custom stuff for html output, but cant seem to find any example of custom pdf output.
Specifically, I'd be interested in customizing the title page, headers, footers, and possibly adding a few pages of front matter before the contents.
Has anyone seen any examples of this kind of customization, or do people pretty much just use the "stock" sphinx output when generating pdfs? Thanks!
Yes i think better or clearer documentation for latex styling would be nice and
some examples.
Here is a question on the sphinx mailing list, which is about about using packages.
sphinx has its own latex documentclass in a file called sphinxmanual.cls or
sphinxhowto.cls, if i remember correctly its a report documentclass that is used as base.
then there is 2 style files, which are the packages, it is those files contents
that are called on when you write \usepackage{mylatexstilefile}
Question about using latex packages
http://groups.google.com/group/sphinx-dev/browse_thread/thread/890dab5e53fff004
Here is my conf.py.
preamb_old = ur'''
%\documentclass{memoir}
\makeatletter
\fancypagestyle{normal}{
\fancyhf{}
\fancyfoot[LE,RO]{{\py#HeaderFamily\thepage}}
\fancyfoot[LO]{{\py#HeaderFamily\nouppercase{\rightmark}}}
\fancyfoot[RE]{{\py#HeaderFamily\nouppercase{\leftmark}}}
\fancyhead[LE,RO]{{\py#HeaderFamily \#title}} % here's the change
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
}
\makeatother
%\color {blue}
%\normalcolor {dark blue}
\pagecolor [RGB]{255, 247, 226}
\definecolor{VerbatimColor}{rgb}{0.95,0.85,0.65}
\definecolor{VerbatimBorderColor}{rgb}{0.5,0.95,0.1}
'''
f = open('graph/static/latexstyling.tex', 'r+')
PREAMBLE = f.read();
latex_elements = {
'papersize':'a4paper',
'pointsize':'11pt',
'classoptions': ',openany',
'babel': '\\usepackage[english]{babel}',
'preamble': PREAMBLE
}
i don't use preamble_old anymore, but it is an example of changes directly in
conf.py, to your latex preamble.
Check out latex_elements and latex_additional_files in the sphinx docs as
a starting point.

hide an entry from Toc in latex

I would like to know how I can hide a section from the table of contents but without loosing the section number in the body of the document. For example, in this tex file I loose the number for hide, and all the sequences are damaged:
\documentclass{article}
\begin{document}
\tableofcontents
\section{uno}
\section{dos}
\section*{hide}
\section{tres}
\end{document}
I think you are looking for
\section*{hide}
\addtocounter{section}{1}
or make it into a command:
\newcommand{\toclesssection}[1]{\section*{#1}\addtocounter{section}{1}}
EDIT:
Okay, I think I understand what is wanted now (and it makes more sense then the answer I gave). Here is a command that you can use to suppress adding a section, subsection, etc. to the TOC. The idea is to temporarily disable \addcontentsline.
\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}
...
\tocless\section{hide}
\tocless\subsection{subhide}
Just wanted to say thanks for Ivans great hint!
(I was just googling for something similar for my customized (Sub)Appendix{} commands:
\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}
\newcommand{\Appendix}[1]{
\refstepcounter{section}
\section*{Appendix \thesection:\hspace*{1.5ex} #1}
\addcontentsline{toc}{section}{Appendix \thesection}
}
\newcommand{\SubAppendix}[1]{\tocless\subsection{#1}}
Maybe this is useful for someone else, too...)
have just come here from a similar question. The answer above didn't quite work as it gave some formatting issues, but a similar solution seemed to do the trick

URL latex linebreak

When a URL is over a line and goes onto a second line, the hyperlink only uses the first line. Is there a way to get around this and yet still have the URL on multiple lines?
You should use the url package:
\usepackage{url}
...
\url{http://long.url.com/... .../stuff}
and it will break it for you at an appropriate place.
I suggest using package:
\usepackage{xurl}
This works fine for me on overleaf.
Example:
\url{https://www.tudelft.nl/ewi/over-de-faculteit/afdelingen/intelligent-systems/pattern-recognition-bioinformatics/pattern-recognition-laboratory/data-and-software/dd-tools/}
At the preamble, just put \usepackage{breakurl} somewhere after \usepackage{hyperref}. The \burl command is defined and, by default, the package also turns the \url command into a synonym of \burl.
Do it the following way. You are good to go!
\begin{document}
\sloppy
For me only this worked:
\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}
and with this code to add line breaks after every normal alphabetic character and with *-~'":
\expandafter\def\expandafter\UrlBreaks\expandafter{\UrlBreaks% save the current one
\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j%
\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t%
\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D%
\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N%
\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X%
\do\Y\do\Z\do\*\do\-\do\~\do\'\do\"\do\-}%
answer complited from this site:
https://tex.stackexchange.com/questions/3033/forcing-linebreaks-in-url/10401
What packages (hyperref,url) and drivers (pdftex, dvips, etc.) are you using? Are you using the breaklinks option to hyperref?
According to the TeX FAQ you might want to try the breakurl package, though it appears you may simply be out of luck in some cases.
What worked best for me (with pdflatex) was
\usepackage[pdftex]{hyperref}
and then just \url{http://stackoverflow.com/} to create URLs. It only broke URLs at . and /, which was okay for my document. Importantly, other approaches I tried created PDFs where only the first line of the URL was a clickable link, and the destination URL was truncated to this portion, which is pretty confusing and bad.
Long URLs that contain underscores (_) in path segments will not be broken when using \PassOptionsToPackage{hyphens}{url}\usepackage{hyperref}. However, I found that I could solve this problem by manually breaking lines in the displayed label using \\\\, like this:
\href{http://www.researchgate.net/profile/Nick\_Brooks2/publication/220012411\_Cultural\_responses\_to\_aridity\_in\_the\_Middle\_Holocene\_and\_increased\_social\_complexity/links/0922b4f398ad68bc96000000.pdf}{http://www.researchgate.net/profile/Nick\_Brooks2/publication/220012411\_\\Cultural\_responses\_to\_aridity\_in\_the\_Middle\_Holocene\_and\_increased\_social\_\\complexity/links/0922b4f398ad68bc96000000.pdf}
This results in URLs that are a bit prettier (IMO) than the use of \usepackage{breakurl} and \burl, as suggested by Debajyoti Mondal, and allows you to display the full URL if you wish.
There is a simple answer. Use \usepackage[hidelinks]{hyperref} before \begin{document}

Resources