我想让文本位于两个相邻的彩色框中。我发现\tcbox
,但它会自动添加换行符。
请注意:我可以使用任何可以与之配合的东西pdflatex
。不需要\tcbox
,它只是最接近我所寻找的东西。
最小示例
\documentclass[a4paper]{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{sectsty}
\usepackage{tcolorbox}
\definecolor{darkblue}{HTML}{00008A}
\definecolor{lightblue}{HTML}{C8C8FA}
\sectionfont{\color{darkblue}}
\subsectionfont{\color{darkblue}}
\tcbset{colback=darkblue,arc=0mm,auto outer arc,boxrule=0mm,width=1.5cm, height=1cm,center title,box align=base}
\pagestyle{fancy}
\fancyhf{}
\rhead{\textcolor{darkblue}{2017}}
\lhead{\tcbox{\textcolor{white}1 \textcolor{white}\textbar}\tcbox[colback=lightblue]{Chapter Name}}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\section{And so it begins}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero
eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no
sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{document}
渲染最小示例
答案1
您缺少该on line
选项,但还需要在两个框中设置一个支柱,因此文本基线也会对齐。
我会使用\newtcbox
而不是全局的\tcbset
。别忘了设置\headheight
(fancyhdr
建议 28.45274pt,我四舍五入为 30pt)。
请注意,KOMA 课程声称fancyhdr
不建议与它们一起使用;您应该查阅指南以便用于scrlayer-scrpage
工作。
\documentclass[a4paper]{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{sectsty}
\usepackage{tcolorbox}
\definecolor{darkblue}{HTML}{00008A}
\definecolor{lightblue}{HTML}{C8C8FA}
\sectionfont{\color{darkblue}}
\subsectionfont{\color{darkblue}}
\newtcbox{\headtcbox}[1][]{%
colback=darkblue,
arc=0mm,
auto outer arc,
boxrule=0mm,
width=1.5cm,
height=1cm,
center title,
box align=base,
on line,
#1,
}
\pagestyle{fancy}
\fancyhf{}
\rhead{\textcolor{darkblue}{2017}}
\lhead{%
\headtcbox{\strut\textcolor{white}{1 \textbar}}%
\headtcbox[colback=lightblue]{\strut Chapter Name}%
}
\setlength{\headheight}{30pt}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\section{And so it begins}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero
eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no
sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{document}
答案2
一个简单的tabular
彩色列就可以了:
\documentclass[a4paper]{article}%{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{graphicx}
\usepackage{sectsty}
\usepackage[table]{xcolor}
\definecolor{darkblue}{HTML}{00008A}
\definecolor{lightblue}{HTML}{C8C8FA}
\sectionfont{\color{darkblue}}
\subsectionfont{\color{darkblue}}
\setlength\headheight{14pt}
\pagestyle{fancy}
\fancyhf{}
\rhead{\textcolor{darkblue}{2017}}
\lhead{\setlength\extrarowheight{2pt}\begin{tabular}{@{}>{\columncolor{darkblue}[0em][\tabcolsep]\color{white}\bfseries\enspace }c>{\cellcolor{lightblue}}l}
1 \textbar & Chapter Name
\end{tabular}}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\section{And so it begins}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero
eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no
sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{document}
答案3
值得千言万语的 MWE:
\documentclass{article}
\usepackage{xcolor}
\begin{document}
\fcolorbox{orange}{yellow}{\strut foo}
\fcolorbox{orange}{yellow}{\strut foo}
\fcolorbox{orange}{yellow}{foo}
\fcolorbox{orange}{yellow}{oops}
\setlength\fboxsep{1em}
\fcolorbox{orange}{yellow}{oops}
\setlength\fboxrule{3pt}
\fcolorbox{orange}{yellow}{oops}
\end{document}
如果您不想让方框之间有空格,请将方框连接在同一行或%
在方框后添加。否则 LaTeX 会将换行符视为空格。
当然,您 \strut
也可以通过 添加垂直填充\colorbox,
,但不是整个内部填充 ,或者 使用白色规则 \fboxsep
添加外部填充。\fboxrule