Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to right justify some code as a text wrap for a memo on the C language and I encountered some issues...

In the following exemple I'm trying to right justify the "const struct tm *ptm);" part as shown below.

size_t strftime(char *dest, size_t taillemax, const char *format,
                                                                      const struct tm *ptm);

I tried the \raggedleft and the flushright environnement. The latter creates a too important separation between the too lines. And I couldn't get \raggedleft to do something.

\documentclass[10pt,a4paper,twoside]{article}
\setlength{\hoffset}{-18pt}
\setlength{\oddsidemargin}{0pt}
\setlength{\evensidemargin}{9pt}
\setlength{\marginparwidth}{54pt
\setlength{\textwidth}{481pt}
\setlength{\voffset}{-18pt}
\setlength{\marginparsep}{7pt}
\setlength{\topmargin}{0pt}
\setlength{\headheight}{13pt}
\setlength{\headsep}{10pt}
\setlength{\footskip}{27pt}
\setlength{\textheight}{680pt}
\setlength{\parskip}{2ex plus .4ex minus .4ex}
\setlength{\parindent}{0pt}
\usepackage{times}
\usepackage[latin9]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{authblk}
\usepackage{listings}
\usepackage{lastpage}
\usepackage{makeidx}
\makeindex
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead[\leftmark]{}
\chead[]{}
\rhead[]{\leftmark}
\lfoot[\thepage\ sur \pageref{LastPage}]{}
\cfoot[]{}
\rfoot[]{\thepage\ sur \pageref{LastPage}}
\usepackage[titles]{tocloft}

\newcommand\fichier[1]{{\textit{#1}}}
\newcommand\code[1]{{\texttt{#1}}}
\newcommand\tab[1]{{\tt\hspace{#1em}}}

\newenvironment{codepar}{\begin{list}{}{\leftmargin20pt}\item[]\obeylines\obeyspaces\begingroup\parskip0pt\tt}{\endgroup\end{list}\par}

\newcommand\codedescr[2]{{ \noindent\code{#1} \begin{list}{}{\leftmargin20pt}\item[]\begingroup\parskip0pt #2 \endgroup\end{list}\par }}

\newcommand{\superscript}[1]{\ensuremath{^{\texttt{#1}}}}

\renewcommand{\arraystretch}{1.3}

\begin{document}

\codedescr{char *ctime( const time\_t *pSec );\newline
char *asctime( const struct tm *ptm );\newline
size\_t strftime(char *dest, size\_t taillemax, const char *format,\newline
\hfil const struct tm *ptm);\index{fonction2@fonction!ctime}\index{fonction2@fonction!asctime}\index{fonction2@fonction!strftime}}{bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla}

\end{document}

Thank you for your help, Cédric

share|improve this question

3 Answers 3

Instead of \hfil or \hfill use

\hspace*{\fill}

\hfill is equivalent to \hspace\fill. However, LaTeX removes horizontal space under circumstances, such as at the end of a line. If you don't want LaTeX to remove this space, use the starred version \hspace*. Btw, it's similar with \vspace and \vspace*.

Btw. when I tested it with your code to see that it works, I noticed a missing closing brace in line 5. And I would use the geometry package instead of manually adjusting all those lengths.

share|improve this answer

I would define differently the macros:

\newcommand\fichier[1]{\textit{#1}}
\newcommand\code[1]{\texttt{#1}}
\newcommand\tab[1]{{\ttfamily\hspace*{#1em}}}

\newenvironment{codepar}
  {\renewcommand{\breakfill}[1]{\par\hspace*{\fill}}%
   \begin{list}{}{\leftmargin20pt \parskip0pt} 
   \item[]\obeylines\obeyspaces\ttfamily\ignorespaces}
  {\end{list}}

\newcommand\codedescr[2]{%
  \par\noindent\code{#1}%
  \begin{list}{}{\leftmargin20pt \parskip0pt} 
  \item[]#2\end{list}}

\newcommand{\superscript}[1]{\ensuremath{^{\texttt{#1}}}}
\newcommand{\breakfill}{\newline\hspace*{\fill}}

The command \breakfill is what you're looking for in order to push a line to the right margin:

\codedescr{char *ctime( const time\_t *pSec );\newline
char *asctime( const struct tm *ptm );\newline
size\_t strftime(char *dest, size\_t taillemax, const char *format,\breakfill
 const struct tm *ptm);%
\index{fonction2@fonction!ctime}%
\index{fonction2@fonction!asctime}%
\index{fonction2@fonction!strftime}}
  {bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
   bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
   bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla}

In the codepar environment I redefine \breakfill so that it doesn't print an empty line (but the command must be used at the end of a line, in that case).

Notice also the different place for \parskip=0pt; I've also cut many useless groups: an environment forms always a group.

Don't use \tt, but rather \ttfamily. The former is an obsolete command.

share|improve this answer

Oh well. To display code there is the listings package. Start by taking at look at the documentation. And consider using the geometry package to adjust the page layout.

Edit: I just noticed a spelling error, where you write \hfil. It should be \hfill.

share|improve this answer
    
Actually, \hfil and \hfill are different commands that insert space with different "springiness". –  Adam Crume Oct 10 '13 at 22:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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