粗体、颜色和居中文本的新命令中的变量存在问题

粗体、颜色和居中文本的新命令中的变量存在问题

我正在尝试输入一个新命令,使某些文本居中、加粗并将文本颜色设为红色。以下是我通过修改和查看已有的代码得出的结果:

\newcommand{\rub}{\large\bfseries\fontfamily{pbk}\selectfont\color{red}\filcenter}

我制定了新的命令\rub,到目前为止一切可以正常工作,只是花括号内的文本未居中。我该如何通过新命令修复以使其居中?

我确实尝试了以下操作:

{\newcommand{\rub}{\large\bfseries\fontfamily{pbk}\selectfont\color{red}\filcenter}{}{0em}{#1}

但出现错误:

You can't use `macro parameter character #' in horizontal mode. ...\selectfont\color{red}\filcenter}{}{0em}{#

但我不太确定这意味着什么。

\documentclass[a4paper,12pt]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage{indentfirst}
\usepackage[explicit]{titlesec}
\usepackage[left=2.00cm,right=2.00cm,top=1.50cm,bottom=2.50cm]{geometry}

\titleformat{\chapter}
{\normalfont\LARGE\bfseries\fontfamily{uncl}\selectfont\filcenter}{}{0em}{#1}

\titleformat{\section}         {\normalfont\LARGE\bfseries\fontfamily{pbk}\selectfont\color{red}\filcenter}{}{0em}{#1}

\titleformat{\subsection}
{\normalfont\Large\bfseries\fontfamily{phv}\selectfont\filcenter}{}{0em}{#1}

\newcommand{\scrip}{\Large\fontfamily{pzc}\selectfont{}}

\newcommand{\rub}        {\large\bfseries\fontfamily{pbk}\selectfont\color{red}\filcenter}

\begin{document}
\chapter{First Sunday after Pentecost -- Sunday of All Saints}

\section{GREAT VESPERS}

\subsection{The Reading from the Wisdom of Solomon.\\
(5:15-6:3a)}

\scrip{15 But
the righteous live for evermore; their reward also is with the
Lord, and the care of them is with the most High. 16 Therefore shall they ...  ... and ill dealing shall overthrow the
stones of the mighty.

{\rub{CHAPTER 6}}

1 Hear therefore, O ye kings, and understand; learn, ye that be judges
of the ends of the earth. 2 Give ear, ye that rule the people, and glory in the
multitude of nations. 3a For power is given to you of the Lord, and sovereignty
from the Highest.}

\end{document}

答案1

抓住提供的参数\rub并将其设置在宽度为的居中框内\linewidth

\newcommand{\rub}[1]{\noindent\makebox[\linewidth]{\large\bfseries\fontfamily{pbk}\selectfont\color{red}#1}}

这是一个最小的例子(没有字体会发生变化,因为它不是讨论的一部分):

在此处输入图片描述

\documentclass{report}

\usepackage{xcolor}
\usepackage{indentfirst}
\usepackage[explicit]{titlesec}

\titleformat{\chapter}
  {\normalfont\LARGE\bfseries\filcenter}{}{0em}{#1}

\titleformat{\section}
  {\normalfont\LARGE\bfseries\color{red}\filcenter}{}{0em}{#1}

\titleformat{\subsection}
  {\normalfont\Large\bfseries\filcenter}{}{0em}{#1}

\newcommand{\scrip}[1]{{\Large #1}}

\newcommand{\rub}[1]{\noindent\makebox[\linewidth]{\large\bfseries\color{red}#1}}

\begin{document}
\chapter{First Sunday after Pentecost -- Sunday of All Saints}

\section{GREAT VESPERS}

\subsection{The Reading from the Wisdom of Solomon.\\
(5:15-6:3a)}

\scrip{15 But the righteous live for evermore; their reward also is with the
Lord, and the care of them is with the most High. 16 Therefore shall they \ldots
and ill dealing shall overthrow the stones of the mighty.

\rub{CHAPTER 6}

1 Hear therefore, O ye kings, and understand; learn, ye that be judges
of the ends of the earth. 2 Give ear, ye that rule the people, and glory in the
multitude of nations. 3a For power is given to you of the Lord, and sovereignty
from the Highest.}

\end{document}

答案2

\newcommand*\rub[1]
  {\begin{center}\large\bfseries\color{red}\fontfamily{pbk}\selectfont#1\end{center}}

...

\rub{This text will be centered, boldfaced, colored red and with font `pbk'}

但由于您没有说要用它做什么,我们不知道是否有更精确和正确的方法。

答案3

这是你想要的吗?

\documentclass[a4paper,12pt]{report}

\usepackage{titlesec}
\usepackage{xcolor}

\begin{document}

\newcommand{\rub}{\large\bfseries\fontfamily{pbk}\selectfont\color{red}\filcenter}

\rub Mytext

\end{document}

如果您想使用 as \rub{only inside here will be affected},那么:

\newcommand{\rub}[1]{\large\bfseries\fontfamily{pbk}\selectfont\color{red}\filcenter{#1}}

答案4

这是一个不使用center环境的解决方案,它增加了不必要的垂直空间。

输出

在此处输入图片描述

代码

\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{lipsum}
\newcommand{\rub}[1]{%
    \par\hfil{%
    \centering%
    \large%
    \bfseries%
    \fontfamily{pbk}%
    \selectfont%
    \color{red}%
    #1%
    }\hfil\par}
\begin{document}%
\rub{Short text}%
\lipsum[1]
\rub{\lipsum[2]}

\end{document}

相关内容