我目前正在用 xelatex 写一本书,在创建几个突出显示框的过程中,我遇到了一个问题。
有些盒子有标题,有些没有,因此盒子的图标会放错位置。
我尝试过使用\ifx
,但是没有作用。
以下是 MWE:
% !TEX encoding = UTF-8
% !TEX spellcheck = es_ES
\PassOptionsToPackage{x11names,table,usenames}{xcolor}
\documentclass[a4]{article}
\usepackage{lipsum}
\usepackage{tikz} % Para poder dibujar
\usetikzlibrary{calc,matrix,shapes,arrows,automata,positioning,graphs,arrows.meta,decorations.pathreplacing,svg.path,shapes.symbols,shadows,backgrounds}
\usepackage{fontawesome}
\usepackage[most]{tcolorbox}
\usepackage{fontspec}
\setmonofont{Iosevka}[Scale=0.9]
\usepackage{calc}
\usepackage{xcolor}
%----------------------------------------------------------------------------------------
% Highlight Boxes
%----------------------------------------------------------------------------------------
\newfontfamily\titleboxfont{Fira Sans} % Font
\newfontfamily\boxfont{Fira Sans Light} % Font
\tcbset{mybox/.style={
enlarge top initially by=5mm,
enlarge bottom finally by=5mm,
fonttitle=\titleboxfont\bfseries,
drop fuzzy shadow,
height=3cm,
height plus=10cm,
enhanced,
% center,
left=0pt,
right=0pt,
top=8pt,
bottom=8pt,
enlarge left by=0mm,
boxsep=5pt,
fontupper=\small\boxfont,
arc=4pt,
outer arc=4pt,
leftupper=1.5cm,
width=0.9\linewidth,
title={#1}
}
}
\colorlet{boxinfocolor}{blue}
\newtcolorbox{pinfobox}[1]{
mybox=#1,
colback=boxinfocolor!5!white,
colframe=boxinfocolor!75!black,
overlay={
% \ifx #1\empty
% \node[anchor=north west] at ([xshift=10pt,yshift=-0.65\baselineskip]frame.north west)
% \else
\node[anchor=north west] at ([xshift=10pt,yshift=-2.65\baselineskip]frame.north west)
% \fi
{{\color{boxinfocolor!75!black}\fontsize{10mm}{10mm}\selectfont\faInfoCircle}};
}
}
\newenvironment{infobox}[1]{%
\begin{figure}[htp]
\begin{pinfobox}{#1}
}
{
\end{pinfobox}
\end{figure}
}
\begin{document}
\begin{infobox}{Lipsum}
\lipsum[1]
\end{infobox}
\begin{infobox}{}
\lipsum[2]
\end{infobox}
\end{document}
在下面的图片中你可以看到正在发生的事情。
感谢您的帮助。
答案1
欢迎来到 TeX.SE!您的测试不起作用,因为当#1
为空时,它会变成:
\ifx \empty
\node[anchor=north west] ...
对于 TeX 来说,这与以下内容完全相同:
\ifx\empty\node[anchor=north west] ...
因此,TeX\empty
与之比较\node
,结果始终为 false。您想要执行的操作可能是这样的:
\def\tmp{#1}%
\ifx\tmp\empty
〈something〉
\else
〈something else〉
\fi
这有效,您可以使用它。对于这段代码,\tmp
当且仅当为空时,宏才定义为使用空替换文本#1
。因此,将的含义\tmp
与的含义进行比较\empty
是有意义的。以下是另一种测试是否#1
为空并根据结果执行代码的方法:
\if\relax\detokenize{#1}\relax
〈something〉
\else
〈something else〉
\fi
除此之外,我:
添加括号
#1
以防mybox={#1}
有人使用#1
包含逗号的值;将您的标题参数转换为默认为空的可选参数,并且
从你的代码中删除了很多未使用的东西。请发布一个最小下次举例。
以下是完整的示例:
\documentclass{article}
\usepackage{fontawesome}
\usepackage{tikz}
\usepackage[skins]{tcolorbox}
\usepackage{lipsum} % only for demo code
\tcbset{
mybox/.style={
enhanced,
enlarge top initially by=5mm,
enlarge bottom finally by=5mm,
fonttitle=\bfseries,
drop fuzzy shadow,
height=3cm,
height plus=10cm,
left=0pt,
right=0pt,
top=8pt,
bottom=8pt,
enlarge left by=0mm,
boxsep=5pt,
fontupper=\small,
arc=4pt,
outer arc=4pt,
leftupper=1.5cm,
width=0.9\linewidth,
title={#1},
}
}
\colorlet{boxinfocolor}{blue}
\newtcolorbox{pinfobox}[1][]{
mybox={#1},
colback=boxinfocolor!5!white,
colframe=boxinfocolor!75!black,
overlay={
\if\relax\detokenize{#1}\relax
\def\myFactor{0.9} % <--- space token which doesn't matter
\else
\def\myFactor{2.65} % <--- ditto
\fi
\node[anchor=north west] at
([xshift=10pt, yshift=-\myFactor\baselineskip]frame.north west)
{%
\color{boxinfocolor!75!black}\fontsize{10mm}{10mm}\selectfont
\faInfoCircle
};
}
}
\newenvironment{infobox}[1][]
{\begin{figure}[htp]\begin{pinfobox}[#1]}
{\end{pinfobox}\end{figure}}
\begin{document}
\begin{infobox}[Lipsum]
\lipsum[1]
\end{infobox}
\begin{infobox}[]
\lipsum[2]
\end{infobox}
\begin{infobox}
\lipsum[3]
\end{infobox}
\end{document}
上述空间标记并不重要,因为它们发生在 Ti 之间钾Z 语句 - 但是如果这让您感觉更舒服,请随意添加行尾百分号。:-)
答案2
我认为参数应该是可选的。你可以更轻松地做到这一点xparse
:缺少可选参数会使\IfNoValueTF
返回“真实路径”
\documentclass[a4paper]{article}
\usepackage[x11names,table,usenames]{xcolor}
\usepackage{tikz} % Para poder dibujar
\usetikzlibrary{
calc,matrix,shapes,arrows,automata,positioning,
graphs,arrows.meta,decorations.pathreplacing,
svg.path,shapes.symbols,shadows,backgrounds
}
\usepackage{fontawesome}
\usepackage[most]{tcolorbox}
\tcbuselibrary{xparse}
\usepackage{fontspec}
\usepackage{lipsum}
\setmonofont{Iosevka}[Scale=0.9]
\newfontfamily\titleboxfont{Fira Sans} % Font
\newfontfamily\boxfont{Fira Sans Light} % Font
%----------------------------------------------------------------------------------------
% Highlight Boxes
%----------------------------------------------------------------------------------------
\tcbset{
pablobox/.style={
enlarge top initially by=5mm,
enlarge bottom finally by=5mm,
fonttitle=\titleboxfont\bfseries,
drop fuzzy shadow,
height=3cm,
height plus=10cm,
enhanced,
%center,
left=0pt,
right=0pt,
top=8pt,
bottom=8pt,
enlarge left by=0mm,
boxsep=5pt,
fontupper=\small\boxfont,
arc=4pt,
outer arc=4pt,
leftupper=1.5cm,
width=0.9\linewidth,
title={#1}
},
}
\colorlet{boxinfocolor}{blue}
\NewTColorBox{pinfobox}{o}{
IfNoValueTF={#1}{pablobox={}}{pablobox={#1}},
overlay={
\node[anchor=north west] at
([xshift=10pt,yshift=-\IfValueTF{#1}{2}{0}.65\baselineskip]frame.north west)
{\textcolor{boxinfocolor!75!black}{\fontsize{10mm}{0pt}\selectfont\faInfoCircle}};
},
colback=boxinfocolor!5!white,
colframe=boxinfocolor!75!black,
}
\NewDocumentEnvironment{infobox}{}
{\begin{figure}[htp]\begin{pinfobox}}
{\end{pinfobox}\end{figure}}
\begin{document}
\begin{infobox}[Lipsum]
\lipsum[1]
\end{infobox}
\begin{infobox}
\lipsum[2]
\end{infobox}
\end{document}
答案3
使用interior
节点并忘记条件问题。
参见geometry nodes
文档tcolorbox
。它解释了 atcolorbox
定义了四个nodes
:(frame
标题+框内容)、interior
(框内容)title
和segmentation
。使用interior
作为放置参考是带有 o 但没有标题的框的有效解决方案。
% !TEX encoding = UTF-8
% !TEX spellcheck = es_ES
\PassOptionsToPackage{x11names,table,usenames}{xcolor}
\documentclass[a4]{article}
\usepackage{lipsum}
\usepackage{tikz} % Para poder dibujar
\usetikzlibrary{calc,matrix,shapes,arrows,automata,positioning,graphs,arrows.meta,decorations.pathreplacing,svg.path,shapes.symbols,shadows,backgrounds}
\usepackage{fontawesome}
\usepackage[most]{tcolorbox}
%\usepackage{fontspec}
%\setmonofont{Iosevka}[Scale=0.9]
\usepackage{calc}
\usepackage{xcolor}
%----------------------------------------------------------------------------------------
% Highlight Boxes
%----------------------------------------------------------------------------------------
%\newfontfamily\titleboxfont{Fira Sans} % Font
%\newfontfamily\boxfont{Fira Sans Light} % Font
\tcbset{mybox/.style={
enlarge top initially by=5mm,
enlarge bottom finally by=5mm,
% fonttitle=\titleboxfont\bfseries,
drop fuzzy shadow,
height=3cm,
height plus=10cm,
enhanced,
% center,
left=0pt,
right=0pt,
top=8pt,
bottom=8pt,
enlarge left by=0mm,
boxsep=5pt,
% fontupper=\small\boxfont,
arc=4pt,
outer arc=4pt,
leftupper=1.5cm,
width=0.9\linewidth,
title={#1}
}
}
\colorlet{boxinfocolor}{blue}
\newtcolorbox{pinfobox}[1]{
mybox=#1,
colback=boxinfocolor!5!white,
colframe=boxinfocolor!75!black,
overlay={
\node[anchor=north west] at ([shift={(10pt,-10pt)}]interior.north west)
{{\color{boxinfocolor!75!black}\fontsize{10mm}{10mm}\selectfont\faInfoCircle}};
}
}
\newenvironment{infobox}[1]{%
\begin{figure}[htp]
\begin{pinfobox}{#1}
}
{
\end{pinfobox}
\end{figure}
}
\begin{document}
\begin{infobox}{Lipsum}
\lipsum[1]
\end{infobox}
\begin{infobox}{}
\lipsum[2]
\end{infobox}
\end{document}