简化我在文件中写地址的方式

简化我在文件中写地址的方式

写信或写文章时,我经常需要在文档开头或结尾打印我的地址。我的做法很丑陋,如下所示:

\documentclass{article}
\begin{document}
\noindent My name\\
\noindent My university\\
\noindent My department\\
\noindent My address\\
\noindent My e-mail\\
\end{document}

显然,一定有比对每一行都重复执行更好的方法\noindent ... \\。我想我可以定义一个新命令\myaddress来简单地打印所有这些行:

\documentclass{article}
% define address command here
\begin{document}
\myaddress
\end{document}

或者至少定义一个负责缩进和换行的新环境:

\documentclass{article}
% define address environment here
\begin{document}
    \begin{address}
        My name
        My university
        My department
        My address
        My e-mail
    \end{address}
\end{document}

但我也不知道该怎么做。

答案1

你可以很容易地做一些简单的事情

\newcommand\printmyaddress{%
\begingroup
\parindent 0pt
\myname       \par
\myuniversity \par
\mydepartment \par
\myaddress    \par
\myemail      \par
\endgroup}

然后相应地设置这些宏。将其放入文件中.sty,您就可以开始了(假设格式化已按您的需要完成)。

不过,也许更详细一点会更有趣。一个想法是使用键值方法,它允许您拥有默认值,如果您愿意,可以逐个覆盖这些默认值。这种方式有很多选择;下面是使用 的一种l3keys

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xparse}

% from here to \ExplSyntaxOff, you could put this in a separate .sty
% to keep things tidy...
\ExplSyntaxOn
% get things set up with defaults:
\keys_define:nn { svdetails }
 {
  name       .tl_gset:N = \g_svdetails_name_tl,
  university .tl_gset:N = \g_svdetails_university_tl,
  department .tl_gset:N = \g_svdetails_department_tl,
  address    .tl_gset:N = \g_svdetails_address_tl,
  email      .tl_gset:N = \g_svdetails_email_tl,
% set the default values (change to real values) 
  name       .initial:n = NAMEDEFAULT,
  university .initial:n = UNIDEFAULT,
  department .initial:n = DEPTDEFAULT,
  address    .initial:n = ADDRESSDEFAULT,
  email      .initial:n = EMAILDEFAULT
 }

\NewDocumentCommand{\biodetails}{ m }
 {
  \keys_set:nn { svdetails } { #1 }
 }

% How do we want things to look when printed? 
% (Nothing fancy here: just 'zero' the \parindent and print
% everything in a new paragraph. But it would be easy to change.)
\NewDocumentCommand{\printdetails}{ }
 {\begingroup
   \parindent 0pt
   \g_svdetails_name_tl       \par
   \g_svdetails_university_tl \par
   \g_svdetails_department_tl \par
   \g_svdetails_address_tl    \par
   \g_svdetails_email_tl      \par
   \endgroup}

\ExplSyntaxOff

% override document defaults; not strictly necessary since we
% already set defaults above...
\biodetails{
  name=Sverre Sverre,
  university=University of Somewhere,
  department=Department of XYZ,
%  address=Unknown Address,
  [email protected],
}

\begin{document}

\printdetails

\end{document}

答案2

也许

\documentclass{article}

\newcommand*{\myaddress}{%
\begin{flushleft}
\setlength{\tabcolsep}{0pt}%  For flushing at least a little bit
\begin{tabular}{l}
My Name \\
My university \\
My department \\
My address \\
My e-mail \\
\end{tabular}
\end{flushleft}
}% End of \newcommand*

\begin{document}
  \myaddress
\end{document}

\arraystretch是一种可能的“快速”解决方案。如果需要,可以通过重新定义来设置行距。

答案3

左对齐\Longstack在这里适用。根据您对项目垂直放置方式的需求,\Longunderstack可能比 更可取\Longstack,但在许多情况下,选择哪个都无所谓。

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\begin{document}
\noindent\Longstack[l]{
 My name\\
 My university\\
 My department\\
 My address\\
 My e-mail}
\textbf{versus}
\Longunderstack[l]{
 My name\\
 My university\\
 My department\\
 My address\\
 My e-mail}
\end{document}

在此处输入图片描述

答案4

根据这里的建议和 Ulrike 的评论取消在页面顶部使用 \baselineskip,下面似乎是一个很好的解决方案。

\documentclass{article}
\newcommand{\myaddress}{\par\vspace{\baselineskip}\vbox{\parindent = 0em%
    My name\\%
    My university\\%
    My department\\%
    My address\\%
    My e-mail}}
\begin{document}
\myaddress
\end{document}

相关内容