将一个文本左对齐,将另一个文本右对齐,但在同一行

将一个文本左对齐,将另一个文本右对齐,但在同一行

我的 cls 文件有以下内容

\newcommand{\jobentry}[5]{
    {\raggedleft\textsc{#1\expandafter\ifstrequal\expandafter{#2}{}{}{\hspace{6pt}\footnotesize{(#2)}}}\par} % Duration and conditional full time/part time text
    \expandafter\ifstrequal\expandafter{#3}{}{}{{\raggedright\large #3}\\} % Employer
    \expandafter\ifstrequal\expandafter{#4}{}{}{{\raggedright\large\textit{\textbf{#4}}}\\[4pt]} % Job title
    \expandafter\ifstrequal\expandafter{#5}{}{}{#5} % Description
    \medskip % Vertical whitespace
}

在我的 tex 文件中,有以下内容

\jobentry
    {Jun 2021 -- Apr 2023} % Duration
    {FT} % FT/PT (full time or part time)
    {ABC Corp} % Employer
    {Software Engineer} % Job title
    {In this summer job I was tasked with helping eradicate pests from industrial areas. Work involved setting traps, spraying and eradication. } % Description

这会在 pdf 上产生以下输出。

在此处输入图片描述

然而,我想ABC 公司在左边和2021 年 6 月 — 2023 年 4 月(全天)就像现在这样在右边,但我希望它们都在同一条线上。我该如何实现呢?

答案1

(首先我要说的是,我认为 #3、#4 或 #5 不应该为空白,因此我只会测试 #2 是否为空白。)

将 BanDoP 的解决方案应用于 cls 文件并得到类似 的内容并不太难{\large#3}\hfill\textsc{#1\ifstrequal{#2}{}{}{\hspace{6pt}\footnotesize(#2)}}\\。(另请注意,这\footnotesize是一个类似 的开关\large,因此您不需要{}。您也不需要\expandafters。但您现在需要一个\noindent。)这是\jobentryOne

[]您可以使用而不是 来强调 #2 是可选的{},这样如果不需要,您可以省略它。这是\jobentryTwo

\documentclass{article}

\usepackage{etoolbox} % needed for \ifstrequal
% \usepackage{xparse} % needed for \NewDocumentCommand if your TeX version isn't new enough

\newcommand{\jobentryZero}[5]{
    {\raggedleft\textsc{#1\expandafter\ifstrequal\expandafter{#2}{}{}{\hspace{6pt}\footnotesize{(#2)}}}\par} % Duration and conditional full time/part time text
    \expandafter\ifstrequal\expandafter{#3}{}{}{{\raggedright\large #3}\\} % Employer
    \expandafter\ifstrequal\expandafter{#4}{}{}{{\raggedright\large\textit{\textbf{#4}}}\\[4pt]} % Job title
    \expandafter\ifstrequal\expandafter{#5}{}{}{#5} % Description
    \medskip % Vertical whitespace
}

\newcommand{\jobentryOne}[5]{%
    \noindent
    {\large#3}\hfill\textsc{#1\ifstrequal{#2}{}{}{\hspace{6pt}\footnotesize(#2)}}\\
    {\raggedright\large\textit{\textbf{#4}}}\smallskip\\ % Job title
    #5 % Description
    \medskip % Vertical whitespace
}

\NewDocumentCommand{\jobentryTwo}{mommm}{%
    \noindent
    {\large#3}\hfill\textsc{#1\IfValueT{#2}{\hspace{6pt}\footnotesize(#2)}}\\
    {\raggedright\large\textit{\textbf{#4}}}\smallskip\\ % Job title
    #5 % Description
    \medskip % Vertical whitespace
}

\begin{document}

\jobentryZero
    {Jun 2021 -- Apr 2023} % Duration
    {FT} % FT/PT (full time or part time)
    {ABC Corp} % Employer
    {Software Engineer} % Job title
    {In this summer job I was tasked with helping eradicate pests from industrial areas. Work involved setting traps, spraying and eradication. } % Description

\jobentryOne
    {Jun 2021 -- Apr 2023} % Duration
    {FT} % FT/PT (full time or part time)
    {ABC Corp} % Employer
    {Software Engineer} % Job title
    {In this summer job I was tasked with helping eradicate pests from industrial areas. Work involved setting traps, spraying and eradication. } % Description

\jobentryTwo
    {Jun 2021 -- Apr 2023} % Duration
    [FT] % FT/PT (full time or part time)
    {ABC Corp} % Employer
    {Software Engineer} % Job title
    {In this summer job I was tasked with helping eradicate pests from industrial areas. Work involved setting traps, spraying and eradication. } % Description

\jobentryTwo
    {Jun 2021 -- Apr 2023} % Duration
    {ABC Corp} % Employer
    {Software Engineer} % Job title
    {In this summer job I was tasked with helping eradicate pests from industrial areas. Work involved setting traps, spraying and eradication. } % Description

\end{document}

代码输出

答案2

已编辑
像这样?

在此处输入图片描述

对于上述结果, 的定义\jobentry可以更简单。它不需要使用etoolbox及其\ifstrequal宏和任何expandafter

假设代码\jobentry,命令中的所有字段始终存在(从问题中看不清楚):

\documentclass{article}
\usepackage{lipsum}
\newcommand{\jobentry}[5]%
{\par\medskip\setlength\parindent{0pt}
    {\raggedright\large #3}                         % Employer
        \hfill
    {\footnotesize\textsc{#1}\hspace{6pt} (#2)}\par % Duration and conditional full time/part time text
    {\large\textit{\textbf{#4}}}\\[4pt]             % Job title
    {#5}                                            % Description
    \medskip                                        % Vertical whitespace
}


\begin{document}
\lipsum[66]
\jobentry%
    {Jun 2021 -- Apr 2023}  % Duration
    {FT}                    % FT/PT (full time or part time)
    {ABC Corp}              % Employer
    {Software Engineer}     % Job title
    {In this summer job I was tasked with helping eradicate pests from industrial areas. Work involved setting traps, spraying and eradication. } % Description
    
\jobentry
    {Jun 2021 -- Apr 2023}  % Duration
    {FT}                    % FT/PT (full time or part time)
    {ABC Corp}              % Employer
    {Software Engineer}     % Job title
    {In this summer job I was tasked with helping eradicate pests from industrial areas. Work involved setting traps, spraying and eradication. } % Description
\end{document}

在此处输入图片描述

答案3

你可以简单地使用\hfill,例如,

Jun 2021 -- Apr 2023 (FT) \hfill ABC Corp\\
Software Engineer

相关内容