I want to adhere to the traditional '80 character' line length limit in my .tex
source file.
How do I do a new line in the source but not in the document output?
sourcecode
I want to adhere to the traditional '80 character' line length limit in my .tex
source file.
How do I do a new line in the source but not in the document output?
Under the assumption that you want, not only to reference, but also to typeset your C++ source code somewhere in your document, you can use the listings
package, define a character to escape to LaTeX (left quote in my example), and write a \label{...}
in your source code within a pair of such escape characters. Just make sure you choose your escape character wisely; if it occurs in places where you don't intend to escape to LaTeX, you'll run into trouble when you try to typeset your code.
If you plan to always place your LaTeX labels within an end-of-line C++ comment, and you don't want those comments to be printed in your PDF, you can define a listings
"to-the-end-of-the-line" delimiter to uniquely identify such comments, using, for instance,
moredelim=[il][]{//latexlabel}
The listings
package will then detect any occurence of //latexlabel
and will not print it; note that listings
will print any unescaped text that follows //latexlabel
, though.
Feel free to customise/shorten that delimiter; //latexlabel
may be a bit too long for your taste.
\documentclass{article}
\usepackage{filecontents} % only to write your code to hello.cpp
\begin{filecontents*}{hello.cpp}
#include <iostream>
int main() {
std::cout << "Hello World" << std::endl; //latexlabel `\label{sc:greet}`
return 0; // another comment that does get typeset
}
\end{filecontents*}
\usepackage{listings}
\lstset
{
language=C++,
escapechar=`,
numbers=left,
moredelim=[il][]{//latexlabel},
}
\begin{document}
\section{Comments on the implementation}
In this program, line~\ref{sc:greet} greets the user.
\section{Source code}
\lstinputlisting{hello.cpp}
\end{document}
I'll use an example from the TeXBook source
This manual is intended for people who have never used \TeX\ before, as
well as for experienced \TeX\ hackers. In other words, it's supposed to
be a panacea that satisfies everybody, at the risk of satisfying nobody.
Everything you need to know about \TeX\ is explained
here somewhere, and so are a lot of things that most users don't care about.
If you are preparing a simple manuscript, you won't need to
learn much about \TeX\ at all; on the other hand, some
things that go into the printing of technical books are inherently
difficult, and if you wish to achieve more complex effects you
will want to penetrate some of \TeX's darker corners. In order
to make it possible for many types of users to read this manual
effectively, a special sign is used to designate material that is
for wizards only: When the symbol
$$\vbox{\hbox{\dbend}\vskip 11pt}$$
appears at the beginning of a paragraph, it warns of a ``^{dangerous bend}''
in the train of thought; don't read the paragraph unless you need to.
Brave and experienced drivers at the controls of \TeX\ will gradually enter
more and more of these hazardous areas, but for most applications the
details won't matter.
the lines here are all less than 80 characters wide, I can show it here and everyone sees the same thing. I (or diff or similar tools) can refer to "experienced" in line 2, and everyone sees the same linebreaks and the whole whole thing is understandable.
If it was all one line it would look like
This manual is intended for people who have never used \TeX\ before, as well as for experienced \TeX\ hackers. In other words, it's supposed to be a panacea that satisfies everybody, at the risk of satisfying nobody. Everything you need to know about \TeX\ is explained here somewhere, and so are a lot of things that most users don't care about. If you are preparing a simple manuscript, you won't need to learn much about \TeX\ at all; on the other hand, some things that go into the printing of technical books are inherently difficult, and if you wish to achieve more complex effects you will want to penetrate some of \TeX's darker corners. In order to make it possible for many types of users to read this manual effectively, a special sign is used to designate material that is for wizards only: When the symbol $$\vbox{\hbox{\dbend}\vskip 11pt}$$ appears at the beginning of a paragraph, it warns of a ``^{dangerous bend}'' in the train of thought; don't read the paragraph unless you need to. Brave and experienced drivers at the controls of \TeX\ will gradually enter more and more of these hazardous areas, but for most applications the details won't matter.
which is OK for TeX but looks horrible here
Even if your editor "soft wraps" to (say) 70 characters it would look like
This manual is intended for people who have never used \TeX\ before,
as well as for experienced \TeX\ hackers. In other words, it's
supposed to be a panacea that satisfies everybody, at the risk of
satisfying nobody. Everything you need to know about \TeX\ is
explained here somewhere, and so are a lot of things that most users
don't care about. If you are preparing a simple manuscript, you won't
need to learn much about \TeX\ at all; on the other hand, some things
that go into the printing of technical books are inherently difficult,
and if you wish to achieve more complex effects you will want to
penetrate some of \TeX's darker corners. In order to make it possible
for many types of users to read this manual effectively, a special
sign is used to designate material that is for wizards only: When the
symbol $$\vbox{\hbox{\dbend}\vskip 11pt}$$ appears at the beginning of
a paragraph, it warns of a ``^{dangerous bend}'' in the train of
thought; don't read the paragraph unless you need to. Brave and
experienced drivers at the controls of \TeX\ will gradually enter more
and more of these hazardous areas, but for most applications the
details won't matter.
but the apparent line breaks come in more arbitrary places and as they are not really there different tools and different people would see different line breaks.
You could add a linebreak at major punctuation (here ,.;:
and $$
math display)
This manual is intended for people who have never used \TeX\ before,
as well as for experienced \TeX\ hackers.
In other words,
it's supposed to be a panacea that satisfies everybody,
at the risk of satisfying nobody.
Everything you need to know about \TeX\ is explained here somewhere,
and so are a lot of things that most users don't care about.
If you are preparing a simple manuscript,
you won't need to learn much about \TeX\ at all; on the other hand,
some things that go into the printing of technical books are inherently difficult,
and if you wish to achieve more complex effects you will want to penetrate some of \TeX's darker corners.
In order to make it possible for many types of users to read this manual effectively,
a special sign is used to designate material that is for wizards only:
When the symbol
$$\vbox{\hbox{\dbend}\vskip 11pt}$$
appears at the beginning of a paragraph,
it warns of a ``^{dangerous bend}'' in the train of thought;
don't read the paragraph unless you need to.
Brave and experienced drivers at the controls of \TeX\ will gradually enter more and more of these hazardous areas,
but for most applications the details won't matter.
which is sort of OK, but still has some over-long lines and just makes editing harder as you edit the punctuation you have to merge lines.
Best Answer
From the TeXbook, p. 25
Remember also that spaces are ignored at the start of a line and that a sequence of spaces is equivalent to one. However, a blank line will count as an end of paragraph command.