1

I am writing a book with relatively long code listings. How can I keep the code contiguous and not have text and figures inserted, which break the flow of the listing. The only way I have found so far is to use \include{} at the end of the chapter.

EDIT:

It would appear that adding a comment was not correct, my mistake. The TeX code is:

Only the operative methods are shown, the header file in the first covers this example as well, Listing \ref{lst:MultilineInt}.

\begin{lstlisting}[caption={Multiline Intersection},label={lst:MultilineInt}]

\end{lstlisting}
\section{Parametric Equations}
\lstinputlisting{lst-MultilineInt.tex}
Before we can discuss the features of \texttt{QPainterPath} that can be used in interactive graphics manipulation, we need to

enter image description here

That shows one problem but there are others where figures are inserted. What else do you need?

This is what you asked for:

\documentclass[12pt]{book}
\usepackage{tikz}
\usepackage{wrapfig, geometry} 
\usepackage{tkz-euclide}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{subcaption} 
\usepackage{marvosym}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{newpxtext} 
\lstset{%
basicstyle=\footnotesize,
language=C++
}
\usepackage[bottom]{footmisc}
\begin{document}
\chapter{2D Interactive Graphics}
%\input{TestInteractiveGraphics}
\begin{figure}
\caption{The Parametric Point on a Line Segment}
\centering
\includegraphics[scale=0.3]{/home/bobwilliams/img/ParametricLine}
\label{fig:line}
\end{figure}

\lstinputlisting[caption={Points from Parametric Equations--Circle},label={lst:parmeqncircle}]{lst-parmeqncircle.tex}
\begin{figure}
\caption{The Parametric Point on a Circle}
\centering
\includegraphics[scale=0.3]{/home/bobwilliams/img/ParametricCircle}
\label{fig:circle}
\end{figure}


\begin{lstlisting}[caption={Points from Parametric Equations--Ellipse},label={lst:parmeqnellipse}]

\end{lstlisting}
\lstinputlisting{lst-parmeqnellipse.tex}
\begin{figure}
\caption{The Parametric Point on a Ellipse}
\centering
\includegraphics[scale=0.3]{/home/bobwilliams/img/ParametricEllipse}
\label{fig:ellipse}
\end{figure}
\end{document}

The files you requested:

Circle
    void MyCanvas::arc()
{
    qreal a{200.0};
    qreal b{200.0};
    QPointF pb{300.0, 250.0};
    qreal t{0.0};
    qreal dt{qDegreesToRadians(360.0)/ 20.0};
    for (int i = 0; i < 21; i++){
        points[i] = pb + QPointF((a*qCos(t)), (b*qSin(t)));
        t = t + dt;
    }
    QPainterPath linePath;
    linePath.arcMoveTo(100.0, 50.0, 400.0, 400.0, 0.0);
    linePath.arcTo(100.0, 50.0, 400.0, 400.0, 0.0, 360.0);
    linePath.moveTo(300.0, 0.0);
    linePath.lineTo(300.0, 1000.0);
    linePath.moveTo(0.0, 250.0);
    linePath.lineTo(1000.0, 250.0);
    linePath.addRect(100.0, 50.0, 400.0, 400.0);
    path = linePath;
    this->update();
}

Ellipse

void MyCanvas::elliptical()
{
    qreal a{150.0};
    qreal b{100.0};
    QPointF pb{300.0, 250.0};
    qreal t{0.0};
    qreal dt{qDegreesToRadians(360.0)/ 20.0};
    for (int i = 0; i < 20; i++){
        points[i] = pb + QPointF((a*qCos(t)), (b*qSin(t)));
        t = t + dt;
    }
    QPainterPath linePath;
    linePath.arcMoveTo(150.0, 150.0, 300.0, 200.0, 0.0);
    linePath.arcTo(150.0, 150.0, 300.0, 200.0, 0.0, 360.0);
    linePath.moveTo(300.0, 0.0);
    linePath.lineTo(300.0, 1000.0);
    linePath.moveTo(0.0, 250.0);
    linePath.lineTo(1000.0, 250.0);
    linePath.addRect(150.0, 150.0, 300.0, 200.0);
    path = linePath;
    this->update();
}

This figure is inserted into the Arc listing:

\begin{figure}
\begin{tikzpicture}
\draw [dashed](0,0) circle (5);
\draw [dashed](0,0) circle (2);
\draw [thick](0,0) ellipse (5 and 2);
\draw (0,0) -- (30:5);
\draw (0,0) -- ({sin(30) * 5},0);
\draw (0,0) -- (0, {cos(30) * 2});
\draw ({sin(30) * 5},0) -- ({sin(30) * 5},{cos(30) * 2});
\draw (0, {cos(30) * 2}) -- ({sin(30) * 5}, {cos(30) * 2});
\draw [->] (0.5,0) arc (0:30:0.5);
\node at (0.75,0.2) {t};
\node at (1.25,-0.2) {$a\cos t$};
\node at (-0.8,0.8) {$b\sin t$};
\node at ({cos(30) * 3.2}, {sin(30) * 3.8}) {$P$};
\node at (0,-3) {x-axis $a = 5$};
\node at (0, -3.5) {y-axis $b = 2$};
\end{tikzpicture}
\caption{Relation of $t$ to the Point on the Ellipse}
\label{fig:delahire}
\end{figure}

Ignore the above figure, I used \captionof{figure} and it solved the problem.

  • Welcome to TeX.SX! – Tom Jan 27 at 17:54
  • See my edit for an updated answer! – Tom Jan 30 at 11:33
1

As you haven't specified anything closer, I suggest the listings package.

\documentclass{article}
\usepackage{listings}
\usepackage{graphicx} % for the example image
\begin{document}

    \begin{figure}[bht]
        \includegraphics[width=4cm]{example-image-a}
    \end{figure}

    \lstinputlisting[language=TeX]{source.tex}

\end{document}

which keeps the source code together in one (at least I could get it to break). Change TeX and source.tex according to your needs. source.tex is an actual file in the directoy of my .tex file. It can be any format you like (not just .tex) .

Take a look at the example output:

example output with listings package

(I am aware that the example source code format is not actually pretty)

Addendum

In the code you have now provided, you are using the listings package incorrectly. You have the listings environment (which is empty), then add a section and then another (different) listing. Instead of this, use the following code, which only uses one listing.

\documentclass{article}
\usepackage{listings}
\begin{document}


This is an example reference: Listing \ref{lst:MultilineInt}.

\lstinputlisting[caption={Multiline Intersection},label={lst:MultilineInt}]{source.tex}

\section{Parametric Equations}

\end{document}

This code results in correct placement of the listing and the remaining elements.

  • Did not work, figures inserted between section of code and a Latex Section was also inserted. More? – REW Jan 28 at 16:24
  • 1
    Can you add a Minimal Working Example, showing your code and the problem? More people will be able to help. – Tom Jan 28 at 17:32
  • I would love to but what is that procedure in this forum for inserting code snippets and screen shots? Can someone point me to the correct location to find that information. Thanks. – REW Jan 28 at 18:31
  • 1
    You can edit your question by clicking the "edit" button. You can then add your code (and format it as code - that's done by selecting it and clicking the button "{}" (alternatively CTRL+K works as well). - The code should be as short as possible, but should of course display your problem. You can read more about making you code here: minimal working example (MWE) – Tom Jan 28 at 22:09
  • The Tex code is :Only the operative methods are shown, the header file in the first covers this example as well, Listing \ref{lst:MultilineInt}. \begin{lstlisting}[caption={Multiline Intersection},label={lst:MultilineInt}] \end{lstlisting} \section{Parametric Equations} \lstinputlisting{lst-MultilineInt.tex} Before we can discuss the features of \texttt{QPainterPath} that can be used in interactive graphics manipulation, we need to------where is the "{}" button and what do I do with the .png file that shows the problem. The reference was of little use. – REW Jan 29 at 15:00
1

Now we have an code we can use. Thanks for adding the needed code to your question!

In your case the best way I think is to not use floating figures. Then the figure will be placed exactly on that place you wrote command \includegraphics. Perhaps you have to move including your file a little bit down or above to get it correctly placed on your page, but that depends at last on the text you have in your document.

Please see that you should change \begin{figure} and \end{figure} to \begin{center} and \end{center} and omit \centering in your old figures, because if not included in a group (like environment center) it would also center your following text.

Because I do not have your images I used option demo for graphicx. I deleted not needed packages for this issue. I added package caption to be able to use command \captionof{figure}{}. Please see that I moved your \label commands to comes directly after \caption where they belong to. At last a remark: figures should have the caption (and the label!) after the image and not before.

Please see the following MWE (I used filecontents to have your c files -- did you see that I renames them to extention c? -- in one compiling code together with the TeX code):

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname-Cirle.c}
void MyCanvas::arc()
{
    qreal a{200.0};
    qreal b{200.0};
    QPointF pb{300.0, 250.0};
    qreal t{0.0};
    qreal dt{qDegreesToRadians(360.0)/ 20.0};
    for (int i = 0; i < 21; i++){
        points[i] = pb + QPointF((a*qCos(t)), (b*qSin(t)));
        t = t + dt;
    }
    QPainterPath linePath;
    linePath.arcMoveTo(100.0, 50.0, 400.0, 400.0, 0.0);
    linePath.arcTo(100.0, 50.0, 400.0, 400.0, 0.0, 360.0);
    linePath.moveTo(300.0, 0.0);
    linePath.lineTo(300.0, 1000.0);
    linePath.moveTo(0.0, 250.0);
    linePath.lineTo(1000.0, 250.0);
    linePath.addRect(100.0, 50.0, 400.0, 400.0);
    path = linePath;
    this->update();
}
\end{filecontents*}

\begin{filecontents*}{\jobname-Ellipse.c}
void MyCanvas::elliptical()
{
    qreal a{150.0};
    qreal b{100.0};
    QPointF pb{300.0, 250.0};
    qreal t{0.0};
    qreal dt{qDegreesToRadians(360.0)/ 20.0};
    for (int i = 0; i < 20; i++){
        points[i] = pb + QPointF((a*qCos(t)), (b*qSin(t)));
        t = t + dt;
    }
    QPainterPath linePath;
    linePath.arcMoveTo(150.0, 150.0, 300.0, 200.0, 0.0);
    linePath.arcTo(150.0, 150.0, 300.0, 200.0, 0.0, 360.0);
    linePath.moveTo(300.0, 0.0);
    linePath.lineTo(300.0, 1000.0);
    linePath.moveTo(0.0, 250.0);
    linePath.lineTo(1000.0, 250.0);
    linePath.addRect(150.0, 150.0, 300.0, 200.0);
    path = linePath;
    this->update();
}
\end{filecontents*}

\documentclass[12pt]{book}

\usepackage{listings}
\usepackage{xcolor}
\usepackage[demo]{graphicx}
\usepackage{caption} % \captionof{figure}{}
\usepackage{newpxtext} 
\usepackage{blindtext} % <======================= to generate dummy text 

\lstset{%
basicstyle=\footnotesize,
language=C++
}


\begin{document}

\chapter{2D Interactive Graphics}
%\input{TestInteractiveGraphics}
\begin{center} % <======================================================
  \captionof{figure}{The Parametric Point on a Circle} % <==============
  \label{fig:circle}
  \includegraphics[scale=0.3]{/home/bobwilliams/img/ParametricCircle}
\end{center} % <========================================================
\lstinputlisting[%
  caption={Points from Parametric Equations--Circle},
  label={lst:parmeqncircle}
]{\jobname-Cirle.c} % lst-parmeqncircle.tex % <=========================

\begin{center}
  \captionof{figure}{The Parametric Point on a Line Segment} % <========
  \label{fig:line}
  \includegraphics[scale=0.3]{/home/bobwilliams/img/ParametricLine}
\end{center}
\lstinputlisting[%
  caption={Points from Parametric Equations--Ellipse},
  label={lst:parmeqnellipse}
]{\jobname-Ellipse.c} % lst-parmeqnellipse.tex % <======================
\begin{figure}
  \caption{The Parametric Point on a Ellipse}
  \label{fig:ellipse}
  \centering
  \includegraphics[scale=0.3]{/home/bobwilliams/img/ParametricEllipse}
\end{figure}

\blindtext

\end{document}

and its result:

resulting pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.