Text FormattingUsing listings, but text goes too far

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
latexforever
Posts: 120
Joined: Fri Nov 14, 2008 9:40 pm

Using listings, but text goes too far

Post by latexforever »

Hello,

I'm using the listings package to put a C code in my document. (it comes from Wikipedia).

Here is what I did:

Code: Select all

    \lstset{language=C, basicstyle=\small, keywordstyle=\bfseries, commentstyle=\itshape, keywords={CORDIC}, emph={for}, emphstyle=\bfseries, numbers=left, stringstyle=\ttfamily, showstringspaces=false, stepnumber=2, numbersep=5pt, showspaces=false, showtabs=false, backgroundcolor=\color{white}}

\begin{lstlisting}[caption={CORDIC},caption=Un exemple de l'algorithme CORDIC implémenté en C,frame=single,title={Un exemple de l'algorithme CORDIC implémenté en C.}]
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    long nb_iter; // Nombre d'itérations
    double K = 0.6073; // Valeur de K
    double x = 1, y = 0; // Valeur approchante de cos(beta) et sin(beta)
    int i; // Compteur de boucle
    double x_Nouveau; // Variable temporaire
    double beta = 0; // Angle à chercher
 
    printf("Calcul par la methode CORDIC de sinus : \n\n\n Veuillez entrer beta\n");
    scanf("%lf",&beta); // entrer la valeur de beta
 
    printf("Veuillez entrer le nombre d'iterations voulues\n");
    scanf("%ld",&nb_iter); // Entrer le nombre d'itération
 
 
    for(i = 0; i < nb_iter; i++) {
         // Si beta<0 rotation dans le sens trigo
         if(beta < 0) {
            x_Nouveau = x + (y*pow(2,-i));
            y -= (x*pow(2,-i));
            beta += atan(pow(2,-i));
         }
         // sinon sans l'autre sens
         else {
            x_Nouveau = x - (y*pow(2,-i));
            y += (x*pow(2,-i));
            beta -= atan(pow(2,-i));
         }
         x = x_Nouveau;
    }
 
    x *= K;
    y *= K;
 
    printf("cos(beta) = %lf , sin(beta) = %lf \n", x,y); // Affichage du résultat
 
}
\end{lstlisting}

Anyway, the border is overlapping with the caption, and ``Veuillez entrer beta\n'' is overlapping with the border. It is a little bit ugly, and I do not always want to make a carriage return to display 4 letters of more.

What can I do to solve these problems?

Thanks.
Last edited by latexforever on Thu Jun 11, 2009 5:00 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Using listings, but text goes too far

Post by localghost »

I can't see the problem. The following example works fine for me.

Code: Select all

\documentclass[11pt,a4paper,french]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{babel}
\usepackage[includeheadfoot,margin=2cm]{geometry}
\usepackage{bera}
\usepackage{listings}

\parindent0em

\begin{document}
\lstset{%
  language=C,
  basicstyle=\small,
  keywordstyle=\bfseries,
  commentstyle=\itshape,
  keywords={CORDIC},
  emph={for},
  emphstyle=\bfseries,
  numbers=left,
  stringstyle=\ttfamily,
  showstringspaces=false,
  stepnumber=2,
  numbersep=5pt,
  showspaces=false,
  showtabs=false
}

\begin{lstlisting}[%
  caption={[CORDIC]Un exemple de l'algorithme CORDIC implémenté en C},
  frame=single,
  title={Un exemple de l'algorithme CORDIC implémenté en C.}
]
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    long nb_iter; // Nombre d'itérations
    double K = 0.6073; // Valeur de K
    double x = 1, y = 0; // Valeur approchante de cos(beta) et sin(beta)
    int i; // Compteur de boucle
    double x_Nouveau; // Variable temporaire
    double beta = 0; // Angle à chercher

    printf("Calcul par la methode CORDIC de sinus : \n\n\n Veuillez entrer beta\n");
    scanf("%lf",β); // entrer la valeur de beta

    printf("Veuillez entrer le nombre d'iterations voulues\n");
    scanf("%ld",&nb_iter); // Entrer le nombre d'itération


    for(i = 0; i < nb_iter; i++) {
         // Si beta<0 rotation dans le sens trigo
         if(beta < 0) {
            x_Nouveau = x + (y*pow(2,-i));
            y -= (x*pow(2,-i));
            beta += atan(pow(2,-i));
         }
         // sinon sans l'autre sens
         else {
            x_Nouveau = x - (y*pow(2,-i));
            y += (x*pow(2,-i));
            beta -= atan(pow(2,-i));
         }
         x = x_Nouveau;
    }

    x *= K;
    y *= K;

    printf("cos(beta) = %lf , sin(beta) = %lf \n", x,y); // Affichage du résultat

}
\end{lstlisting}
\end{document}
Seems to be only a matter of the type area. Perhaps studying Section 4.10 (Margins and line shape, p. 33f) of the listings manual will be helpful.


Best regards
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
latexforever
Posts: 120
Joined: Fri Nov 14, 2008 9:40 pm

Using listings, but text goes too far

Post by latexforever »

Thanks. But the problem comes when I use (modifying your MWE):

Code: Select all

    \documentclass[11pt,a4paper,french]{article}
    \usepackage[latin1]{inputenc}
    \usepackage{babel}
    \usepackage[includeheadfoot,margin=2cm]{geometry}
    %\usepackage{bera}
    \usepackage{listings}

    \parindent0em

    \begin{document}
    \lstset{%
      language=C,
      basicstyle=\small,
      keywordstyle=\bfseries,
      commentstyle=\itshape,
      keywords={CORDIC},
      emph={for},
      emphstyle=\bfseries,
      numbers=left,
      stringstyle=\ttfamily,
      showstringspaces=false,
      stepnumber=2,
      numbersep=5pt,
      showspaces=false,
      showtabs=false
    }

    \begin{lstlisting}[%
      caption={[CORDIC]Un exemple de l'algorithme CORDIC implémenté en C},
      frame=single,
      title={Un exemple de l'algorithme CORDIC implémenté en C.}
    ]
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main()
    {
        long nb_iter; // Nombre d'itérations
        double K = 0.6073; // Valeur de K
        double x = 1, y = 0; // Valeur approchante de cos(beta) et sin(beta)
        int i; // Compteur de boucle
        double x_Nouveau; // Variable temporaire
        double beta = 0; // Angle à chercher

        printf("Calcul par la methode CORDIC de sinus : \n\n\n Veuillez entrer beta\n");
        scanf("%lf",?); // entrer la valeur de beta

        printf("Veuillez entrer le nombre d'iterations voulues\n");
        scanf("%ld",&nb_iter); // Entrer le nombre d'itération


        for(i = 0; i < nb_iter; i++) {
             // Si beta<0 rotation dans le sens trigo
             if(beta < 0) {
                x_Nouveau = x + (y*pow(2,-i));
                y -= (x*pow(2,-i));
                beta += atan(pow(2,-i));
             }
             // sinon sans l'autre sens
             else {
                x_Nouveau = x - (y*pow(2,-i));
                y += (x*pow(2,-i));
                beta -= atan(pow(2,-i));
             }
             x = x_Nouveau;
        }

        x *= K;
        y *= K;

        printf("cos(beta) = %lf , sin(beta) = %lf \n", x,y); // Affichage du résultat

    }
    \end{lstlisting}
    \end{document}
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Re: Using listings, but text goes too far

Post by localghost »

You obviously didn't follow the advice in my last reply concerning the manual.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
latexforever
Posts: 120
Joined: Fri Nov 14, 2008 9:40 pm

Using listings, but text goes too far

Post by latexforever »

localghost wrote:You obviously didn't follow the advice in my last reply concerning the manual.
I think there is a misunderstanding: I have read it, and it now works (e.g. by setting

Code: Select all

breaklines=true
), but I would like to know why it does not work when removing the lines I removed, from your MWE.


Thanks for everything.
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Using listings, but text goes too far

Post by localghost »

The bera font package provides a narrower mono spaced type face than the standard EC fonts.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
latexforever
Posts: 120
Joined: Fri Nov 14, 2008 9:40 pm

Re: Using listings, but text goes too far

Post by latexforever »

Thanks.
Post Reply