4

I have the following code:

\documentclass{article}

\usepackage[brazilian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\DeclareUnicodeCharacter{B0}{\textdegree}
% ---------------------------------------------------------------------------
% Pacotes Opcionais
% ---------------------------------------------------------------------------
\usepackage{rotating}           % Usado para rotacionar o texto
\usepackage[all,knot,arc,import,poly]{xy}   % Pacote para desenhos gráficos
% Este pacote pode conflitar com outros pacotes gráficos como o ``pictex''
% Então é necessário usar apenas um dos pacotes conflitantes
\newcommand{\VerbL}{0.52\textwidth}
\newcommand{\LatL}{0.42\textwidth}
\usepackage{abntex2cite}
\usepackage{tablefootnote}
\usepackage{lettrine}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{scalefnt}
\usepackage{hhline} 
\usepackage{caption}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage{ltxtable} 
\usepackage{longtable}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage[nomargin,inline,marginclue,draft]{fixme}
\usepackage{enumitem} 
\makeatletter
\renewcommand*\FXLayoutInline[3]{%
  {\@fxuseface{inline}\ignorespaces[#3 \fxnotename{#1}: #2]}}
\makeatother

%\usepackage{caption}
\usepackage{subcaption}
%\usepackage{subfig}
\usepackage{float}

\renewcommand\thesubfigure{(\alph{subfigure})}
\makeatletter
\renewcommand{\p@subfigure}{\thefigure~} % unbreakable space after fig. num.
\makeatother

\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}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\usepackage{listings}

\lstset{language=Java,
  basicstyle=\ttfamily\footnotesize,
  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}]{\%\%}{\%\%},
}


\usepackage[newfloat]{minted}
\usepackage{mdframed}
\usepackage{csquotes}
\renewcommand*{\mkcitation}[1]{ [#1]}

% %usar \nome{}
% \newcommand{\ricardo}[1]{\colorbox{ForestGreen}{\color{white}\textsf{\textbf{Ricardo}}} \textcolor{yellow}{#1}}
% \thispagestyle{empty}

\begin{document}

  \begin{lstlisting}[caption={Automated testing of Create, Read, Update and Delete (CRUD) of the Universities of the conventional approach},label=testesgrupoB, language=Java]

public class UniversitySystemTest{

    private PageUniversities pageUniversities;
    private LoadUniversity loadUniversity;

    @Before
    public void initialize() throws Exception {                                 
        pageUniversities = new PageUniversities();                  
        loadUniversity = new LoadUniversity();
        loadUniversity.performDataLoad();       
        pageUniversities.visitListUniversities();       
    }

    @Test
    public void addAUniversity() {  
        pageUniversities.new()
            .register(
                    "University Paulista", "Sao Paulo - SP", 
                    "Street Jardim Cris, n 40, Santo Amaro", "13572-000");      
        assertTrue(pageUniversities
            .existsInTheListing("University Paulista", "Sao Paulo - SP"));          
    }

    @Test
    public void deveRemoverUmaUniversity() {        
        pageUniversities._new().register("University Paulista","Sao Paulo - SP", "Rua Jardim Cris, n 40, Santo Amaro", "13572-000");
        assertTrue(pageUniversities.existsInTheListing("University Paulista", "Sao Paulo - SP"));
        pageUniversities.deleteUniversityInLastPosition();
        assertFalse(pageUniversities.existsInTheListing("University Paulista", "Sao Paulo - SP"));      
    }

    @Test
    public void mustListUniversities() {                
        int quantityOfUniversities = pageUniversities.getUniversityRecords().size();
        assertThat(quantityOfUniversities, is(10));
        assertThat(pageUniversities.getUniversityTableRodapeText(), is(quantityOfUniversities + " Universitys"));       
    }

    @Test
    public void mustEditAUniversity() {     
        pageUniversities._new().register("University Paulista", "Sao Paulo - SP", "Rua Jardim Cris, n 40, Santo Amaro", "13572-000");
        pageUniversities.changes().to("Federal University of Bahia", "Salvador - BA", "University Campus, n 127", " 40210-730");        
        assertFalse(pageUniversities.existsInTheListing("University Paulista", "Sao Paulo - SP"));
        assertTrue(pageUniversities.existsInTheListing("Federal University da Bahia", "Salvador - BA"));        
    }

    @After
    public void finalize(){
        loadUniversity.performDischarge();
    }
}

  \end{lstlisting}

\thispagestyle{empty}

\end{document}

But the code is coming off the margin. According to the following figures:

enter image description here enter image description here

How do I format the Java code correctly so that the code is better indented and within the margin of the page?

  • Might be an idea, if you start by defining what is the correct way to format java code? As it sits not, your question is quite unclear, plus your MWE loads a lot of stuff which is now relevant to your question. – daleif Mar 12 '18 at 15:05
  • looks like you have unballanced quotes (") which confuse the LaTeX formatter as well as the java compiler. Therfore the long line strartlig with "assert" is treaten as a "string word" rather den code and is not wrapped. – Timothy Truckle Mar 12 '18 at 15:13
  • 1
    Hello @daleif and TimothyTruckle, I have corrected the code and were edit my question. What I need to do is indent the best code within the margin of the page. Do you have any idea how I can do this? – ricardoramos Mar 12 '18 at 18:41
  • moredelim needs two mandatory arguments. You only have one in moredelim=[il][\textcolor{pgrey}]{$$}. That could be confusing things. – Nicola Talbot Mar 12 '18 at 18:42
  • Hello @NicolaTalbot, I noticed that there is an error in this line, I have adapted this code and I do not know how to use moredelim correctly. Can you help me, please? – ricardoramos Mar 12 '18 at 18:45
4

I can't reproduce your output, so this is more of a comment than an answer but it might help to track down the problem.

First trim down your MWE to remove all extraneous packages and just keep one of the Java methods that's problematic:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{xcolor}

\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}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\usepackage{listings}

\lstset{language=Java,
  basicstyle=\ttfamily\footnotesize,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily
}

\begin{document}

  \begin{lstlisting}[language=Java]

    @Test
    public void addAUniversity() {  
        pageUniversities.new()
            .register(
                    "University Paulista", "Sao Paulo - SP", 
                    "Street Jardim Cris, n 40, Santo Amaro", "13572-000");      
        assertTrue(pageUniversities
            .existsInTheListing("University Paulista", "Sao Paulo - SP"));          
    }

  \end{lstlisting}

\thispagestyle{empty}

\end{document}

This uses space characters rather than TABs and produces:

image of page

This site doesn't seem to support TABs, so I replaced all instances of 8 spaces with TAB characters. In the following ^I represents a TAB character:

\begin{document}

  \begin{lstlisting}[language=Java]

    @Test
    public void addAUniversity() {  
^IpageUniversities.new()
^I    .register(
^I^I    "University Paulista", "Sao Paulo - SP", 
^I^I    "Street Jardim Cris, n 40, Santo Amaro", "13572-000");      
^IassertTrue(pageUniversities
^I    .existsInTheListing("University Paulista", "Sao Paulo - SP"));^I  
    }

  \end{lstlisting}

\thispagestyle{empty}

\end{document}

This still produces the same output as before, so the next step is to try loading geometry with showframe to show the page layout:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,showframe]{geometry}

\usepackage{xcolor}

\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}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\usepackage{listings}

\lstset{language=Java,
  basicstyle=\ttfamily\footnotesize,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily
}

\begin{document}

  \begin{lstlisting}[language=Java]

    @Test
    public void addAUniversity() {  
        pageUniversities.new()
            .register(
                    "University Paulista", "Sao Paulo - SP", 
                    "Street Jardim Cris, n 40, Santo Amaro", "13572-000");      
        assertTrue(pageUniversities
            .existsInTheListing("University Paulista", "Sao Paulo - SP"));        
    }

  \end{lstlisting}

\thispagestyle{empty}

\end{document}

image of page

If you're still not getting this result, then add \listfiles, which shows the version numbers of all the packages. It may be that you're using a different version that's causing the problem. I get the following list:

 *File List*
 article.cls    2014/09/29 v1.4h Standard LaTeX document class
  size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
inputenc.sty    2015/03/17 v1.2c Input encoding file
    utf8.def    2017/01/28 v1.1t UTF-8 support for inputenc
   t1enc.dfu    2017/01/28 v1.1t UTF-8 support for inputenc
  ot1enc.dfu    2017/01/28 v1.1t UTF-8 support for inputenc
  omsenc.dfu    2017/01/28 v1.1t UTF-8 support for inputenc
 fontenc.sty
   t1enc.def    2017/04/05 v2.0i Standard LaTeX file
geometry.sty    2010/09/12 v5.6 Page Geometry
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
   ifpdf.sty    2017/03/15 v3.2 Provides the ifpdf switch
  ifvtex.sty    2016/05/16 v1.6 Detect VTeX and its facilities (HO)
 ifxetex.sty    2010/09/12 v0.6 Provides ifxetex conditional
  xcolor.sty    2016/05/11 v2.12 LaTeX color extensions (UK)
   color.cfg    2016/01/02 v1.6 sample color configuration
  pdftex.def    2018/01/08 v1.0l Graphics/color driver for pdftex
listings.sty    2015/06/04 1.6 (Carsten Heinz)
 lstmisc.sty    2015/06/04 1.6 (Carsten Heinz)
listings.cfg    2015/06/04 1.6 listings configuration
lstlang1.sty    2015/06/04 1.6 listings language file
atbegshi.sty    2016/06/09 v1.18 At begin shipout hook (HO)
infwarerr.sty    2016/05/16 v1.4 Providing info/warning/error messages (HO)
 ltxcmds.sty    2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
supp-pdf.mkii
  t1cmtt.fd    2014/09/29 v2.5h Standard LaTeX font definitions
 ***********
  • Hello @Nicola, I'm doing the tests here. – ricardoramos Mar 13 '18 at 0:35

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.