如何在不改变布局的情况下更改字体?

如何在不改变布局的情况下更改字体?

我使用模板创建了一个表格。但是,我希望整个表格使用相同的字体,而不是无衬线字体。我尝试将命令从 \textsf 更改为 \text,但这只会使文本继续超出框的范围。

\documentclass{article}
\usepackage{textcomp,,amsmath, longtable}
\usepackage{latexsym}
\begin{document}
    
    %% Set up counters for questions
    %% 
    \newcounter{qnumber}
    \setcounter{qnumber}{0}
    \stepcounter{qnumber}
    \newcommand{\yesno}{\textsf{Yes}~~\textsf{No} {\Large ~$\Box$}~~~~   {\Large $\Box$}}
    
    %% we now define the questions
    \newcommand{\question}[1]{
        \hfill \relax1.\theqnumber\hfill\hfill &\textsf{#1} &{\small\yesno} \stepcounter{qnumber}\\ \hline
    }
    
    %% we now define the questions
    \newcommand{\heading}[1]{
        1.0 &\multicolumn{4}{|l|}{\bf\text{#1}}\\ \hline %
    }
    \newcommand{\divider}{\hline}
    \begin{longtable}{|c| p{10cm}|p{1.5cm}|}
        \divider
        Nr.&Would you get vaccinated in this situation? &  \\
        \divider\divider
        
        \question{Now, one year later, a vaccine is available. If you and enough people in Econotopia get vaccinated, fewer people will die and restrictions will be repealed. Therefore, your actions will have an effect.}
        \question{Because a lot of people took the vaccine, the authorities in Econotopia are introducing a system where vaccinated people will be able to go to restaurants, small concerts and be allowed to work from the office and meet physically at school.}
        \question{To get more people vaccinated, the authorities are now offering 100€ if you get vaccinated.}
        \question{Until now, also unvaccinated people have had the same rights of freedom as vaccinated people if they got tested for the virus 2 times a week. The authorities are now removing this possibility. Therefore, if you are not vaccinated, you cannot go to for example concerts, restaurants, or being able to go to your school or your job physically.}
        \question{Due to a new wave of hospitalization, the authorities in Econotopia introduce more powerful means to get more people vaccinated. Therefore, in addition to not having the same freedom as vaccinated people, you must pay a monthly fee of 50€ if you do not get the vaccine.}
        \question{On the news, the anchor can tell that the side effects from the vaccine can cause death for some people. Therefore, by getting vaccinated you have risks from the side effects as well as the risks from getting the virus.}
        
    \end{longtable}
    
\end{document}

答案1

要不使用无衬线字体,只需完全删除\textsf\text由数学提供amsmath并形成一个框,这就是换行不再起作用的原因)。

我擅自更改了其他一些内容:

  • “是”和“否”下的框现在正确居中
  • 我从你的表中删除了所有丑陋的规则,而是使用了booktabs
  • 我进行了正确的设置longtable(使用\endhead\endfoot)以便在每一页上重复可能需要的第一行和底部的规则longtable
  • 我删除了未使用的\heading命令(该命令包含邪恶的不应使用的指令\bf,十多年来已被弃用)。
  • 我从你的定义中删除了虚假的空格\question(未注释的行尾{和之后}将导致那里出现空格)。
  • 我将您的定义移到序言中,以便更好地将代码与内容分开。
  • 我将其移至\stepcounter更合理的位置并将其更改为\refstepcounter(这样您就可以使用\label以便稍后引用它)。
  • \hfill从中删除了这些命令\question,它们没有任何用处(应在表格序言中指定列对齐)。请参阅\hfill本回答末尾有关这些命令的特殊之处的更多信息。
\documentclass{article}
\usepackage{textcomp,,amsmath, longtable}
\usepackage{booktabs}
\usepackage{latexsym}

%% Set up counters for questions
%% 
\newcounter{qnumber}
\setcounter{qnumber}{0}
\newcommand*\tickbox{{\Large$\Box$}}%
\newcommand*\yesno
  {\begin{tabular}[t]{@{}c@{~~}c@{}}Yes&No\\\tickbox&\tickbox\end{tabular}}%

%% we now define the questions
\newcommand{\question}[1]{%
    \refstepcounter{qnumber}1.\theqnumber
    & #1
    & \small\yesno
    \\
    \addlinespace
}

\begin{document}
    
    
    \begin{longtable}{c p{10cm} c}
      \toprule
        Nr.&Would you get vaccinated in this situation? &  \\
      \midrule
      \endhead
      \bottomrule
      \endfoot
      \question{Now, one year later, a vaccine is available. If you and enough people in Econotopia get vaccinated, fewer people will die and restrictions will be repealed. Therefore, your actions will have an effect.}
      \question{Because a lot of people took the vaccine, the authorities in Econotopia are introducing a system where vaccinated people will be able to go to restaurants, small concerts and be allowed to work from the office and meet physically at school.}
      \question{To get more people vaccinated, the authorities are now offering 100€ if you get vaccinated.}
      \question{Until now, also unvaccinated people have had the same rights of freedom as vaccinated people if they got tested for the virus 2 times a week. The authorities are now removing this possibility. Therefore, if you are not vaccinated, you cannot go to for example concerts, restaurants, or being able to go to your school or your job physically.}
      \question{Due to a new wave of hospitalization, the authorities in Econotopia introduce more powerful means to get more people vaccinated. Therefore, in addition to not having the same freedom as vaccinated people, you must pay a monthly fee of 50€ if you do not get the vaccine.}
      \question{On the news, the anchor can tell that the side effects from the vaccine can cause death for some people. Therefore, by getting vaccinated you have risks from the side effects as well as the risks from getting the virus.}
    \end{longtable}
    
\end{document}

在此处输入图片描述


这些人究竟在做什么\hfill

在原始问题(以及这个答案的早期版本)中,宏\question是通过多个\hfill调用定义的,如下所示:

\newcommand{\question}[1]{%
    \refstepcounter{qnumber}\hfill 1.\theqnumber\hfill\hfill
    & #1
    & \small\yesno\\
    \addlinespace
}

让我们仔细看看它们的作用。首先,它们会覆盖表序言中指定的任何对齐方式(例如,因为c输入的列使用“较弱” \hfil)。这通常没有什么不好,但却违背了在表序言中指定表对齐的目的(在本例中是c这样的)。

那么它们起什么作用呢?每个都\hfill增加了一个具有二阶无穷大拉伸的拉伸空间(比大一个无穷大\hfil,Knuthian TeX 知道三个无穷大)。因此,当表格列比其内容宽时,这些列\hfill将会拉伸(并且\hfilLaTeX 的列使用的不会做任何事情)。因此\hfill\hfill增加了两部分拉伸并\hfill增加了一个。现在人们可能会认为这会导致这种情况下列稍微偏离中心,因为我们\hfill在右边发现了两个,在左边发现了一个。但是在尝试忽略单元格中的尾随空格时,LaTeX 会使用\unskip删除最后一个空格,但在这里第二个空格\hfill被删除了。结果是一列在两边都有一部分拉伸,因此居中,那么为什么要这么麻烦,而不是直接使用一c列呢?

我们怎样才能创建一个稍微偏离中心的列?我们可以通过在第二\hfill个列后面放置一些不是跳过的东西(例如一个空的\mbox{},或一个零宽度\kern0pt)来阻止删除第二个列,或者(我猜这也是这段代码最初出现的原因)我们\hfill在那里放置另一个被 删除的列\unskip

相关内容