[Tex/LaTex] Right justify inline

codehorizontal alignment

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

Best Answer

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.

Related Question