更改文本和标题的字体时出现问题

更改文本和标题的字体时出现问题

我需要将我的字体类型全局设置为 Times New Roman(用于文本)以及 Arial(用于图形和表格标题)。

通常我对乳胶有些熟悉,但在这里我几乎是碰壁了。

我尝试做的几件事之一:将解释器更改为 Xelatex,并在序言中添加以下几行:

\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}

然而,在我进入下一步定义标题之前,我使用 fontspec 的那一刻,就把我为文档所做的所有其他格式都搞乱了。所有内容都变成了奇怪的字体,部分标题不再是粗体,等等。

有没有一种非侵入式的方法来改变字体而不破坏一切?

我尝试用谷歌搜索,结果为此打开了上百万个标签。目前还没有成功。但我不敢相信像更改字体类型这样重要而简单的事情竟然没有一种无忧无虑的方法,所以我来了。提前谢谢您。

附加信息:此项目是合作项目,因此我使用 overleaf。

更多附加信息:我被要求提供非常冗长的序言,所以就在这里。但我很确定问题不在于序言本身,因为一旦 fontenc 被激活,它就会直接忽略这些内容。

\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
%\input{fontchange}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathptmx}       %different looking math font
%\usepackage{mathtimes}
\usepackage{xcolor}
%\usepackage[framed, numbered]{matlab-prettifier} For matlab code
\usepackage{enumitem}
\usepackage{graphicx}
\usepackage{fancyhdr}   % for headers, footers and pagenumbering
\usepackage{multicol}
\usepackage{multirow}
\usepackage{lastpage}   % so {page of page} works
\usepackage{tocloft}   % for table of contents formatting
\usepackage{setspace}   % for additional spacing
\usepackage{float}      % for images and figures
%\usepackage{cleveref}   % for referencing subfigs
\usepackage{subcaption} % no need to explauin
%\usepackage[flushleft]{threeparttable}  %for notes under tables
%\usepackage[english]{nomencl}
\usepackage{lipsum}     % dummy text package
\usepackage{siunitx}
\usepackage{array}
\usepackage{tabularx}
\usepackage{pifont}      %allows use of special characters, use \ding{xx}
\usepackage{caption}
\usepackage{subcaption}  %for sub figure environment
\usepackage{titlesec}    %package for formatting title spacing !
\usepackage{parskip}     % paragraph skip
\usepackage{geometry}        % Document margins
\usepackage{todonotes}   % self explanatory   
%\usepackage[sc]{helvet} %helvetica text
\usepackage{graphicx}   % Allows including images
\usepackage{booktabs}   % Allows the use of \toprule, \midrule and \bottomrule in tables
\usepackage[final]{pdfpages} %for including pdf's 
% Hyper References
\usepackage[hidelinks]{hyperref}
\hypersetup{
    colorlinks=true,
    citecolor=black,
    urlcolor=black, %blue for blue obv.
    linkcolor=black}
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode} %for matlab code
\usepackage{mwe}
% References
\usepackage[backend=biber,
            style=numeric,
            citestyle=ieee,
            %backref=true,
            abbreviate=false,
            dateabbrev=false,
            alldates=long]{biblatex}
\addbibresource{sources.bib} %
%\usepackage{showframe}      % used to show the document size
\usepackage{multicol}   %for several columns in a document
\setlength{\columnsep}{7mm}
\addtolength{\skip\footins}{10pt plus 3pt minus 3pt}    %spacing of footnotes
\renewcommand\footnoterule{\kern -3pt \hrule width 50mm \kern 2.6pt}
%Geometry of the pagelayout
\geometry{a4paper, textheight=720pt, left=25mm, right=25mm, top=25mm, footskip=12mm, headsep=6mm}
\setlength {\marginparwidth }{22mm} % for the todonotes package

%---------------- DOCUMENT FORMATING -------------------------
%spacing of white vertical space above and below equations
\setlength{\abovedisplayskip}{14pt plus 2pt minus 2pt}
\setlength{\belowdisplayskip}{14pt plus 2pt minus 2pt}

%%%% FORMAT CAPTIONS %%%%%%
\usepackage[font=footnotesize,labelfont=bf,labelsep=period,textfont=sl]{caption}
\captionsetup[table]{skip=3pt, position=top}
\captionsetup[figure]{skip=6pt, belowskip=0pt}
\captionsetup[subfigure]{skip=6pt, belowskip=0pt}
\setlength{\intextsep}{14pt plus 2pt minus 2pt} %spacing between
%\setlength{\floatsep}{20pt plus 1.0pt minus 2.0pt}
\setlength{\captionmargin}{20pt}                % caption woun't go full width

%%%%%%%% Format headings %%%%%%%%
\titlespacing{\section}      {0em}{2.5em}{.6em}
\titlespacing{\subsection}   {0em}{1.50em}{.2em}
\titlespacing{\subsubsection}{0em}{1.0em}{0.2em}
\titlespacing{\paragraph}    {0em}{.25em}{.25em}

\setlength{\parskip}{0.em}           %length of paragraph skip
%%%%% Format font size %%%%%%%%%%
\titleformat{\Section}
  {\normalfont\large\bfseries}{\thesection}{1em}{}
  
\titleformat{\Subsection}
  {\normalfont\normalsize\bfseries}{\thesubsection}{1em}{}
 
\titleformat{\subsubsection}
  {\normalfont\normalsize\bfseries}{\thesubsubsection}{1em}{}

\setcounter{secnumdepth}{3}     %number before section title up to 3rd level
\setcounter{tocdepth}{2}        % 2 levels of toc

\counterwithin{table}{section}
\counterwithin{figure}{section}
\numberwithin{equation}{section}
% --- set footer and header ---
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}  %headrule line thickness = 0
\renewcommand{\footrulewidth}{0pt}  % bottomrule line thickness = 0
\fancyhead{}                        % no header
\setlength{\parindent}{0em}
\newlist{steps}{enumerate}{1}
\setlist[steps, 1]{leftmargin=1.5cm,label = Step \arabic*:}
\graphicspath{{graphics/}}

答案1

我只是用 xelatex 包含了一个简单的 MWE 构建来显示一些我经常使用的字幕包选项(虽然不是同时使用的)。

% !TeX TS-program = xelatex

\documentclass[12pt,a4paper]{article}

\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}

\RequirePackage{booktabs}
\RequirePackage{graphicx}

\RequirePackage[%
dvipsnames, % Load a set of predefined colors 
table      % Load the colortbl package
]{xcolor}


\RequirePackage{caption} %lots of options in the manual
\DeclareCaptionFont{gray}{\color{gray}}

\captionsetup[figure]{%
    font={footnotesize,sf},%
    name=Fig.,
    format=hang,
    justification=raggedright,
    margin=15pt,
    indention=0pt}


\captionsetup[table]{%   A
    font=small,%
    labelfont={gray,sf},
    font={footnotesize,sf},%
    textfont=it,
    name=Table,
    labelsep=newline,
    skip=0.5\baselineskip,
    justification=raggedright,
    indention=0pt,
    position=top}

\RequirePackage{kantlipsum} %dummy text

\begin{document}
    
\kant[1]    

\begin{figure}
    \centering
    \includegraphics[height=150pt,keepaspectratio]{example-image-b}
    \caption{Dummy image and caption example B}
    \label{fig:B}
\end{figure}

\begin{center}
\begin{table}[htbp]
    \centering
    \caption{Simple table for testing captions}
    \begin{tabular}{lcccc}
        \toprule
        & Column1 & \multicolumn{2}{c}{Columns 2-3} & Column 4 \\
        \midrule
        Sample & a     & \multicolumn{2}{c}{b} & c \\
        perf  & 123   & \multicolumn{2}{c}{456} & 789 \\
        \rowcolor[rgb]{ .816,  .808,  .808} assess & n/a   & \multicolumn{2}{c}{best} & normal \\
        \bottomrule
    \end{tabular}%
    \label{tab:addlabel}%
\end{table}%
\end{center}
    
\end{document}

在此处输入图片描述

更新

罪魁祸首似乎是 \usepackage{mathptmx}。当您评论它时,其他一切都正常。即使安装了最新版本,它似乎也会带来麻烦。(mathptmx.sty 2020/03/25 PSNFSS-v9.3 Times w/ Math,改进(SPQR,WaS))

我使用您的完整前言编译了我的示例,甚至mcode还添加了一些内容\cite以及我自己的 .bib 文件。我发现的唯一问题是citestyle=ieee,。似乎“biblatex”的最近更新存在问题,很快就会解决,所以现在请对该行进行注释。

% !TeX TS-program = xelatex

\documentclass[12pt,a4paper]{article}

\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}

\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
%\usepackage{mathptmx}       %different looking math font NOT WORKING

\RequirePackage{booktabs}
\RequirePackage{graphicx}   

\RequirePackage[%
dvipsnames, % Load a set of predefined colors 
table      % Load the colortbl package
]{xcolor}

\usepackage{titlesec}    %package for formatting title spacing !
%%%% FORMAT CAPTIONS %%%%%%
\usepackage[font=footnotesize,labelfont=bf,labelsep=period,textfont=sl]{caption}
\captionsetup[table]{skip=3pt, position=top}
\captionsetup[figure]{skip=6pt, belowskip=0pt}
\captionsetup[subfigure]{skip=6pt, belowskip=0pt}
\setlength{\intextsep}{14pt plus 2pt minus 2pt} %spacing between
%\setlength{\floatsep}{20pt plus 1.0pt minus 2.0pt}
\setlength{\captionmargin}{20pt}                % caption woun't go full width

%%%%%%%% Format headings %%%%%%%%
\titlespacing{\section}      {0em}{2.5em}{.6em}
\titlespacing{\subsection}   {0em}{1.50em}{.2em}
\titlespacing{\subsubsection}{0em}{1.0em}{0.2em}
\titlespacing{\paragraph}    {0em}{.25em}{.25em}

\setlength{\parskip}{0.em}           %length of paragraph skip

\RequirePackage{kantlipsum} %dummy text
\RequirePackage{blindtext} %dummy text

\begin{document}        
    
\Blinddocument  
\blindmathpaper

\begin{figure}
    \centering
    \includegraphics[height=150pt,keepaspectratio]{example-image-b}
    \caption{Dummy image and caption example B } %dummy text}}
    \label{fig:B}
\end{figure}

\begin{center}
    \begin{table}
        \centering
        \caption{Simple table for testing captions}
        \begin{tabular}{lcccc}
            \toprule
            & Column1 & \multicolumn{2}{c}{Columns 2-3} & Column 4 \\
            \midrule
            Sample & a     & \multicolumn{2}{c}{b} & c \\
            perf  & 123   & \multicolumn{2}{c}{456} & 789 \\
            \rowcolor[rgb]{ .816,  .808,  .808} assess & n/a   & \multicolumn{2}{c}{best} & normal \\
            \bottomrule
        \end{tabular}%
        \label{tab:addlabel}%
    \end{table}%
\end{center}
    
\end{document}

N1 氮气 N3

否则,正如您已经观察到的,章节标题和标题将会失败。

n4

N5

相关内容