5

Hi i need help for fix this code in the part \begin{lstlisting}[style=Java] i have 2 errors

can read the source code from another file and add a main .tex file?

PD: i am new in LaTeX - PD 2: my english is't good jaja

enter image description here

\documentclass[letterpaper]{article}
\usepackage[letterpaper,margin=0.75in]{geometry}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish,activeacute]{babel}
\selectlanguage{spanish}
\usepackage{times}
\renewcommand{\shorthandsspanish}{}
\usepackage{color}
\definecolor{gray97}{gray}{.97}
\definecolor{gray75}{gray}{.75}
\definecolor{gray45}{gray}{.45}
\usepackage{listings}
\lstset{ frame=Ltb,
framerule=0pt,
aboveskip=0.5cm,
framextopmargin=3pt,
framexbottommargin=3pt,
framexleftmargin=0.4cm,
framesep=0pt,
rulesep=.4pt,
backgroundcolor=\color{gray97},
rulesepcolor=\color{black},
%
stringstyle=\ttfamily,
showstringspaces = false,
basicstyle=\small\ttfamily,
commentstyle=\color{gray45},
keywordstyle=\bfseries,
%
numbers=left,
numbersep=15pt,
numberstyle=\tiny,
numberfirstline = false,
breaklines=true,
}
% 
\lstnewenvironment{listing}[1][]
{\lstset{#1}\pagebreak[0]}{\pagebreak[0]}

\lstdefinestyle{Java}
{language=Java,
}   

\begin{document}

\noindent
Impresión de código fuente del fichero \texttt{Exe4.java}:

\begin{lstlisting}[style=Java]
package exe4;

import java.util.Scanner;

/**
 *
 * @author Felipe Leiva
 */
public class Exe4 {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        Conductor cond = new Conductor();

        cond.impresion();
        cond.limpiarConsola();
        System.out.println("Ingrese el rut del Conductor: ");
        String dato = scan.nextLine();
        cond.setRut(dato);
        System.out.println("Ingrese el nombre del Conductor: ");
        dato = scan.nextLine();
        cond.setNombre(dato);
        System.out.println("Ingrese el apellido del Conductor: ");
        dato = scan.nextLine();
        cond.setApellido(dato);
        System.out.println("Ingrese sexo (M para Masculino/ F para Femenino):"
                + " ");
        char sexo = scan.next().charAt(0);
        cond.setSexo(sexo);
        System.out.println("Ingrese año de contratacion del conductor: ");
        int datoint = scan.nextInt();
        cond.setContratacion(datoint);
        System.out.println("Ingrese el sueldo del conductor: ");
        datoint = scan.nextInt();
        cond.setSueldo(datoint);
        scan.nextLine();
        System.out.println("Ingrese el tipo de contrato (S si es contrato "
                + "indefinido o F si no tiene):");
        dato = scan.nextLine();
        while (!dato.equals("S") & !dato.equals("F")) {
            System.out.println("ERROR - Solo puede ingresar 2 opciones 'S' o "
                    + "'F'");
            System.out.println("");
            System.out.println("Ingrese nuevamente la opcion: ");
            dato = scan.nextLine();
        }
        if (dato.equals("S")) {

            boolean indefinido = true;
            cond.setIndefinido(indefinido);
        }
        if (dato.equals("F")) {
            boolean indefinido = false;
            cond.setIndefinido(indefinido);
        }
        cond.limpiarConsola();
        cond.impresionFormato();
        cond.calcularYears();
        cond.aumentoSueldo();
    }
}
\end{lstlisting}

\end{document}
4

The manual of listings says: Similarly, if you are using UTF-8 extended characters in a listing, they must be placed within an escape to LATEX.

This works (provided you do not use | in your program):

\begin{lstlisting}[style=Java, escapechar={|}]
...
System.out.println("Ingrese |año| de contratacion del conductor: ");
...
\end{lstlisting}

Here we defined | as escape to LaTeX character, and asked listings to typeset año inside LaTeX.

Of course, this also would work, but, I guess, is less readable:

System.out.println("Ingrese a|ñ|o de contratacion del conductor: ");

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.