8

the first c program

I'm trying to create something similar to this, but I am having trouble getting it to work. I have tried using the tabular environment, but i get an "overfull hbox". I have also used tabulary and \hfill. \hfill seems like what I want, but I am having trouble using it across multiple lines. What would be the best way to implement something like the picture in LaTeX?

Here is what i have tried so far:

\verb|#include <stdio.h> \hfill \verb{include information about standard library}
\verb|main| \hfill \emph{define a function named} \verb|main| \emph{that receives no argument values}

and so on, but once the italic text on the right gets too long, it starts to align against the left margins.

3 Answers3

6

Here is a suggestion using listings.

\documentclass{article}
\usepackage{listings}
\lstset{
  language=C,
  frame=tb,
  escapeinside={@:}{:@},
  showstringspaces=false,
  basicstyle=\ttfamily
}

\newcommand\Comment{\hfill\normalfont\itshape}
\begin{document}

\begin{lstlisting}[caption={First C code}]
#include <stdio.h> @:\Comment include information about standard library:@

main() @:\Comment define a function named \texttt{main} :@
@:\Comment that receives no argument values :@
{
    printf("hello world\n");
}

\end{lstlisting}
\end{document}

enter image description here

Torbjørn T.
  • 206,688
4

A variant of Torbjørn's answer, where we can decide the comment box has no height (but some manual adjustment may become necessary).

\documentclass{article}
\usepackage{xparse}
\usepackage{listings}
\lstset{
  language=C,
  frame=tb,
  escapeinside={@:}{:@},
  showstringspaces=false,
  basicstyle=\linespread{1.4}\ttfamily,
  columns=fullflexible,
}

\NewDocumentCommand{\Comment}{O{\depth}m}{%
  \hfill\normalfont\itshape
  \renewcommand{\arraystretch}{0.7}%
  \raisebox{0pt}[\height][#1]{%
    \begin{tabular}[t]{@{}r@{}}
    #2
    \end{tabular}%
  }%
}
\DeclareTextFontCommand{\textttup}{\upshape\ttfamily}
\newcommand{\escape}[1]{\textttup{\symbol{`\\}#1}}

\begin{document}

\begin{lstlisting}[caption={First C code}]
#include <stdio.h>     @:\Comment{include information about standard library}:@
main()                 @:\Comment{define a function named \textttup{main}\\
                                  that receives no argument values}:@
{                      @:\Comment{statements of \textttup{main} are enclosed in braces}:@
    printf("hello world\n"); @:\Comment[0pt]{\textttup{main} calls library function \textttup{printf}\\
                                             to print the sequence of characters;\\
                                             \escape{n} represents the newline character}:@
}
@:\vspace{-.5\baselineskip}:@
\end{lstlisting}
\end{document}

enter image description here

egreg
  • 1,121,712
3

Below I've set the code inside a figure (to allow it to float, if needed), while the structure is defined inside a tabular.

enter image description here

\documentclass{article}

% Insert a code comment: \codecomment{<comment>}
% Comment is set in a [t]op-aligned tabular that has tight
% vertical spacing and also overlaps with content below it.
\newcommand{\codecomment}[1]{{%
  \renewcommand{\arraystretch}{0}%
  \normalfont\itshape\hfill
  \smash{\begin{tabular}[t]{r @{}}
    #1%
  \end{tabular}}%
}}
\newcommand{\codefont}{\normalfont\ttfamily}
\newcommand{\setcode}[1]{{\codefont #1}}

\begin{document}

\begin{figure}
  \renewcommand{\arraystretch}{1.5}% Space code vertically (http://tex.stackexchange.com/a/31704/5764)
  \codefont% Set code font
  \begin{tabular}{p{\dimexpr\linewidth-2\tabcolsep}}
    \hline \\
    \string#include <stdio.h>
      \codecomment{include information about standard library} \\
    main()
      \codecomment{define a function named \setcode{main} \\
                   that receives no argument values} \\
    \string{
      \codecomment{statements of \setcode{main} are enclosed in braces} \\
    \qquad printf("hello, world\string\n");
      \codecomment{\setcode{main} calls library function \setcode{printf} \\
                   to print this sequence of characters; \\
                   \setcode{\string\n} represents the newline character} \\
    \string} \\\\
    \multicolumn{1}{c}{\normalfont A first C program.} \\
    \hline
  \end{tabular}
\end{figure}

\end{document}
Werner
  • 603,163
  • Hi Werner my congratulations for your solution. My answer is not very good, – Sebastiano Dec 23 '16 at 19:17
  • I would comment the line end #1% in the definition of \codecomment, because the tabular code only adds one \unskip. If the user leaves a space before the closing argument brace, then only one space would be removed and the comment is moved by one space to the left. – Heiko Oberdiek Dec 23 '16 at 19:22