如何在同一行上左右对齐

如何在同一行上左右对齐

第一个 C 程序

我正在尝试创建类似的东西,但我无法让它工作。我尝试使用环境tabular,但得到了“overfull hbox”。我也使用过tabulary\hfill\hfill这似乎是我想要的,但我在多行中使用它时遇到了麻烦。在 LaTeX 中实现类似图片的最佳方法是什么?

以下是我迄今为止尝试过的:

\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}

等等,但是一旦右侧的斜体文本太长,它就会开始与左边距对齐。

答案1

以下是使用的建议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}

在此处输入图片描述

答案2

Torbjørn 答案的一个变体,我们可以决定评论框没有高度(但可能需要进行一些手动调整)。

\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}

在此处输入图片描述

答案3

下面我在 a 内部设置了代码figure(以便在需要时允许它浮动),而结构是在 a 内部定义的tabular

在此处输入图片描述

\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}

相关内容