2

I have a situation similar to the following code:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{inconsolata}

\usepackage{color}
\usepackage{graphicx}

\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}

\usepackage{minted}
\usepackage{listings}
\lstset{language=Java,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily,
  moredelim=[il][\textcolor{pgrey}]{$$},
  moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}

\makeatletter
\AtBeginDocument{\let\c@listing\c@lstlisting}
\makeatother

\begin{document}

Eos ea harum labores scaevola, mea autem vivendo interesset ne, vis verterem patrioque at. Et adhuc malis perpetua quo. Aperiri deseruisse qui in, sea nihil aperiri et. Per tale dolore apeirian at, nam reque liber meliore id.

Ne impetus percipitur mea, noster animal sanctus vix et. In ridens sapientem cum, ex cum facer commodo patrioque. Alia tractatos complectitur eam ut. Ius eu luptatum probatus efficiantur, cibo debitis his at, an usu illud numquam iudicabit. In hendrerit interpretaris est, ex eum consequat eloquentiam, est augue scriptorem ut. Et iusto mnesarchum appellantur eam.

Causae appetere te cum, conceptam disputationi te eam. Per ne duis indoctum, ius idque veniam scaevola ne. Nominavi erroribus constituam at pri, ea ius eruditi adversarium, duo omnis quando id. Dicta fabellas persecuti has id, eu sea simul deleniti, ipsum aeterno dolorem an vim. Cu ullum apeirian legendos eos, vim autem appetere ocurreret ut.

Iisque eligendi ius at. Mutat veritus ne mel. Quo cu quis populo. Usu sensibus partiendo interesset ei.

Semper inciderint ut sed, vix id inani recusabo torquatos. Reque impedit assentior est ea, ex maluisset expetendis vel. Wisi albucius philosophia at per. Aeque volutpat ullamcorper ne nec, ea pro bonorum abhorreant. Commodo fuisset sit no.

Ne mei intellegat definitiones, persius atomorum constituam te eam. Te mandamus dissentiet qui, no sanctus lucilius eum, prodesset tincidunt eu has. Duo an movet vivendum eloquentiam, vix te vide eius imperdiet. Case doctus sea at, vim ex sapientem quaerendum. At sale aliquam efficiantur vim.

Adhuc viris aperiam est in, eius postea habemus vis te. Usu et aliquam dissentias. Sea ad melius lucilius lobortis, an has quas adipiscing, est ex delenit partiendo. Alia detraxit vim eu. An eam prima expetenda, per melius incorrupte elaboraret no.

Ne mei intellegat definitiones, persius atomorum constituam te eam. Te mandamus dissentiet qui, no sanctus lucilius eum, prodesset tincidunt eu has. Duo an movet vivendum eloquentiam, vix te vide eius imperdiet. Case doctus sea at, vim ex sapientem quaerendum. At sale aliquam efficiantur vim.

\begin{figure}[!ht]
\centering
%Insere Figura
\includegraphics[width=0.9 \columnwidth]{img/tex.png}
\caption{Tex Stackexchange}
\label{fig:tex}
\end{figure}

\begin{lstlisting}[caption={Description of Java code},label=useless, language=Java]
/**
 * This is a doc comment.
 */
package com.ociweb.jnb.lombok;

import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;

$$@Data
$$@EqualsAndHashCode(exclude={"address","city","state","zip"})
public class Person {
    enum Gender { Male, Female }

    // another comment

    %%@NonNull%% private String firstName;
    %%@NonNull%% private String lastName;
    %%@NonNull%% private final Gender gender;
    %%@NonNull%% private final Date dateOfBirth;

    private String ssn;
    private String address;
    private String city;
    private String state;
    private String zip;
}
\end{lstlisting}

Listing \ref{useless} contains an example of a listing

Euismod meliore duo te, at vivendo officiis per. Ea verterem lucilius urbanitas duo, nibh autem tamquam eu sea. Feugait mediocrem vel an, eu graecis tibique vulputate pro. Ne aperiri facilis scriptorem vix. Ad sed causae virtute antiopam, te usu suas sale neglegentur, tation quidam delenit has ne.

Ut eirmod viderer usu, te eius nulla minimum vix. Elit audire cum id, sit eros erroribus te. An melius blandit concludaturque his. Qui putent accommodare signiferumque cu, tamquam accusam tincidunt his in. Lorem sententiae ad vis.

In quo alii ceteros evertitur. Vim ea officiis corrumpit, est te meis euismod. Nec cu utroque meliore, at reque solet voluptatibus sea. Latine scripta eum ut, id vidit euismod tibique eos.

\end{document}

By dividing the page the image is placed in the middle of code:

enter image description here

How do you position the listing and the image correctly in this case?

2
  • Try with [hb] in \begin{figure}[hb].
    – user11232
    Oct 16, 2015 at 14:34
  • Hello Harish Kumar, I tested the [hb], but plays the figure for the end of the file. Oct 16, 2015 at 14:48

1 Answer 1

1

The answer depends on how do you want to treat the objects and what's the expected final position of one with respect to the other, and this was not specified in the question.

One option is to use the float option for the listing; in this way, it's treated like a float and won't be split (but also won't break across pages):

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{inconsolata}

\usepackage{color}
\usepackage{graphicx}

\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}

\usepackage{minted}
\usepackage{listings}
\lstset{language=Java,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily,
  moredelim=[il][\textcolor{pgrey}]{$$},
  moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}

\makeatletter
\AtBeginDocument{\let\c@listing\c@lstlisting}
\makeatother

\begin{document}

Eos ea harum labores scaevola, mea autem vivendo interesset ne, vis verterem patrioque at. Et adhuc malis perpetua quo. Aperiri deseruisse qui in, sea nihil aperiri et. Per tale dolore apeirian at, nam reque liber meliore id.

Ne impetus percipitur mea, noster animal sanctus vix et. In ridens sapientem cum, ex cum facer commodo patrioque. Alia tractatos complectitur eam ut. Ius eu luptatum probatus efficiantur, cibo debitis his at, an usu illud numquam iudicabit. In hendrerit interpretaris est, ex eum consequat eloquentiam, est augue scriptorem ut. Et iusto mnesarchum appellantur eam.

Causae appetere te cum, conceptam disputationi te eam. Per ne duis indoctum, ius idque veniam scaevola ne. Nominavi erroribus constituam at pri, ea ius eruditi adversarium, duo omnis quando id. Dicta fabellas persecuti has id, eu sea simul deleniti, ipsum aeterno dolorem an vim. Cu ullum apeirian legendos eos, vim autem appetere ocurreret ut.

Iisque eligendi ius at. Mutat veritus ne mel. Quo cu quis populo. Usu sensibus partiendo interesset ei.

Semper inciderint ut sed, vix id inani recusabo torquatos. Reque impedit assentior est ea, ex maluisset expetendis vel. Wisi albucius philosophia at per. Aeque volutpat ullamcorper ne nec, ea pro bonorum abhorreant. Commodo fuisset sit no.

Ne mei intellegat definitiones, persius atomorum constituam te eam. Te mandamus dissentiet qui, no sanctus lucilius eum, prodesset tincidunt eu has. Duo an movet vivendum eloquentiam, vix te vide eius imperdiet. Case doctus sea at, vim ex sapientem quaerendum. At sale aliquam efficiantur vim.

Adhuc viris aperiam est in, eius postea habemus vis te. Usu et aliquam dissentias. Sea ad melius lucilius lobortis, an has quas adipiscing, est ex delenit partiendo. Alia detraxit vim eu. An eam prima expetenda, per melius incorrupte elaboraret no.

Ne mei intellegat definitiones, persius atomorum constituam te eam. Te mandamus dissentiet qui, no sanctus lucilius eum, prodesset tincidunt eu has. Duo an movet vivendum eloquentiam, vix te vide eius imperdiet. Case doctus sea at, vim ex sapientem quaerendum. At sale aliquam efficiantur vim.

\begin{figure}
\centering
%Insere Figura
\includegraphics[width=0.9 \columnwidth]{example-image-a}
\caption{Tex Stackexchange}
\label{fig:tex}
\end{figure}

\begin{lstlisting}[caption={Description of Java code},label=useless, language=Java,float]
/**
 * This is a doc comment.
 */
package com.ociweb.jnb.lombok;

import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;

$$@Data
$$@EqualsAndHashCode(exclude={"address","city","state","zip"})
public class Person {
    enum Gender { Male, Female }

    // another comment

    %%@NonNull%% private String firstName;
    %%@NonNull%% private String lastName;
    %%@NonNull%% private final Gender gender;
    %%@NonNull%% private final Date dateOfBirth;

    private String ssn;
    private String address;
    private String city;
    private String state;
    private String zip;
}
\end{lstlisting}

Listing \ref{useless} contains an example of a listing

Euismod meliore duo te, at vivendo officiis per. Ea verterem lucilius urbanitas duo, nibh autem tamquam eu sea. Feugait mediocrem vel an, eu graecis tibique vulputate pro. Ne aperiri facilis scriptorem vix. Ad sed causae virtute antiopam, te usu suas sale neglegentur, tation quidam delenit has ne.

Ut eirmod viderer usu, te eius nulla minimum vix. Elit audire cum id, sit eros erroribus te. An melius blandit concludaturque his. Qui putent accommodare signiferumque cu, tamquam accusam tincidunt his in. Lorem sententiae ad vis.

In quo alii ceteros evertitur. Vim ea officiis corrumpit, est te meis euismod. Nec cu utroque meliore, at reque solet voluptatibus sea. Latine scripta eum ut, id vidit euismod tibique eos.

\end{document}

enter image description here

Another option would be to use another placement specifier for the figure; for example, with [!hbp] one gets:

enter image description here

Try changing the placement specifier till you get a layout that satisfies your needs.

Yet another option is to treat the figure as a static object inside a minipage, for example, and provide the caption using \¢aptionof from the caption or capt-of packages:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{inconsolata}

\usepackage{color}
\usepackage{graphicx}
\usepackage{caption}

\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}

\usepackage{minted}
\usepackage{listings}
\lstset{language=Java,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily,
  moredelim=[il][\textcolor{pgrey}]{$$},
  moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}

\makeatletter
\AtBeginDocument{\let\c@listing\c@lstlisting}
\makeatother

\begin{document}

Eos ea harum labores scaevola, mea autem vivendo interesset ne, vis verterem patrioque at. Et adhuc malis perpetua quo. Aperiri deseruisse qui in, sea nihil aperiri et. Per tale dolore apeirian at, nam reque liber meliore id.

Ne impetus percipitur mea, noster animal sanctus vix et. In ridens sapientem cum, ex cum facer commodo patrioque. Alia tractatos complectitur eam ut. Ius eu luptatum probatus efficiantur, cibo debitis his at, an usu illud numquam iudicabit. In hendrerit interpretaris est, ex eum consequat eloquentiam, est augue scriptorem ut. Et iusto mnesarchum appellantur eam.

Causae appetere te cum, conceptam disputationi te eam. Per ne duis indoctum, ius idque veniam scaevola ne. Nominavi erroribus constituam at pri, ea ius eruditi adversarium, duo omnis quando id. Dicta fabellas persecuti has id, eu sea simul deleniti, ipsum aeterno dolorem an vim. Cu ullum apeirian legendos eos, vim autem appetere ocurreret ut.

Iisque eligendi ius at. Mutat veritus ne mel. Quo cu quis populo. Usu sensibus partiendo interesset ei.

Semper inciderint ut sed, vix id inani recusabo torquatos. Reque impedit assentior est ea, ex maluisset expetendis vel. Wisi albucius philosophia at per. Aeque volutpat ullamcorper ne nec, ea pro bonorum abhorreant. Commodo fuisset sit no.

Ne mei intellegat definitiones, persius atomorum constituam te eam. Te mandamus dissentiet qui, no sanctus lucilius eum, prodesset tincidunt eu has. Duo an movet vivendum eloquentiam, vix te vide eius imperdiet. Case doctus sea at, vim ex sapientem quaerendum. At sale aliquam efficiantur vim.

Adhuc viris aperiam est in, eius postea habemus vis te. Usu et aliquam dissentias. Sea ad melius lucilius lobortis, an has quas adipiscing, est ex delenit partiendo. Alia detraxit vim eu. An eam prima expetenda, per melius incorrupte elaboraret no.

Ne mei intellegat definitiones, persius atomorum constituam te eam. Te mandamus dissentiet qui, no sanctus lucilius eum, prodesset tincidunt eu has. Duo an movet vivendum eloquentiam, vix te vide eius imperdiet. Case doctus sea at, vim ex sapientem quaerendum. At sale aliquam efficiantur vim.

\begin{lstlisting}[caption={Description of Java code},label=useless, language=Java]
/**
 * This is a doc comment.
 */
package com.ociweb.jnb.lombok;

import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;

$$@Data
$$@EqualsAndHashCode(exclude={"address","city","state","zip"})
public class Person {
    enum Gender { Male, Female }

    // another comment

    %%@NonNull%% private String firstName;
    %%@NonNull%% private String lastName;
    %%@NonNull%% private final Gender gender;
    %%@NonNull%% private final Date dateOfBirth;

    private String ssn;
    private String address;
    private String city;
    private String state;
    private String zip;
}
\end{lstlisting}

\noindent
\begin{minipage}{\textwidth}
\begin{center}
%Insere Figura
\includegraphics[width=0.9 \columnwidth]{example-image-a}
\captionof{figure}{Tex Stackexchange}
\label{fig:tex}
\end{center}
\end{minipage}

Listing \ref{useless} contains an example of a listing

Euismod meliore duo te, at vivendo officiis per. Ea verterem lucilius urbanitas duo, nibh autem tamquam eu sea. Feugait mediocrem vel an, eu graecis tibique vulputate pro. Ne aperiri facilis scriptorem vix. Ad sed causae virtute antiopam, te usu suas sale neglegentur, tation quidam delenit has ne.

Ut eirmod viderer usu, te eius nulla minimum vix. Elit audire cum id, sit eros erroribus te. An melius blandit concludaturque his. Qui putent accommodare signiferumque cu, tamquam accusam tincidunt his in. Lorem sententiae ad vis.

In quo alii ceteros evertitur. Vim ea officiis corrumpit, est te meis euismod. Nec cu utroque meliore, at reque solet voluptatibus sea. Latine scripta eum ut, id vidit euismod tibique eos.

\end{document}

enter image description here

Of course, these modifications should be made once the document is in its final stages.

3
  • Hello Gonzalo Medina, the code can be divided, but would like the image does not appear in the middle of code. I would like it to appear just below the code, but can not change the order of the figure with the listing on my real situation. Oct 16, 2015 at 14:45
  • @ricardoramos please see my updated answer. It's just a metter of choosing the proper placement for the figure using the proper position specifier. Oct 16, 2015 at 15:08
  • Hello Gonzalo Medina, thank you so much for your help, I managed to position the figure and the listing properly. Oct 16, 2015 at 17:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .