必须在环境之前放置一个命令,定义环境的类型。但是,第一个布尔值未执行,结果是错误的。请帮我解决这个问题。
\documentclass[12pt]{article}
\usepackage{etoolbox}
\newbool{blbt}
\newbool{blvd}
%%%=======================
\newenvironment{bt}{\global\setbool{blvd}{false}\global\setbool{blbt}{true}\par}{}
%%%=======================
\newenvironment{vd}{\global\setbool{blvd}{true}\global\setbool{blbt}{false}\par}{}
%%%=======================
\newcommand{\classify}{The below environment is
\ifbool{blvd}{Example}{\ifbool{blbt}{Problem}{Wrong}}}
\begin{document}
{\bfseries\classify}
\begin{bt}
Contents PPP
\end{bt}
{\bfseries\classify}
\begin{vd}
Contents EXEXEX
\end{vd}
\end{document}
答案1
这个怎么样?
\documentclass[12pt]{article}
\usepackage{etoolbox}
\newbool{blbt}
\newbool{blvd}
\newcommand{\classify}{The environment below is
\ifbool{blvd}{Example}{\ifbool{blbt}{Problem}{Wrong}}}
%%%=======================
\newenvironment{bt}{\global\setbool{blvd}{false}\global\setbool{blbt}{true}{\bfseries\classify}\par}{}
%%%=======================
\newenvironment{vd}{\global\setbool{blvd}{true}\global\setbool{blbt}{false}{\bfseries\classify}\par}{}
%%%=======================
\begin{document}
\begin{bt}
Contents PPP
\end{bt}
\begin{vd}
Contents EXEXEX
\end{vd}
\end{document}
答案2
如果你不介意你的命令\classify
和环境bt
并且vt
有一个非可选的参数,那么你可以使用该包参考值并且有
- 环境
bt
和vt
位置引用标签的属性blvd
和blbt
- 该命令
\classify
从这些引用标签中提取属性的值:
\documentclass[12pt]{article}
\usepackage{etoolbox}
\usepackage{zref}
\makeatletter
\@ifdefinable\@zrefpropdefined{%
\ZREF@Robust\def\@zrefpropdefined{%
\zref@wrapper@babel\@ZREFpropdefined
}%
}%
\@ifdefinable\@ZREFpropdefined{%
\def\@ZREFpropdefined#1#2#3#4{%
% #1 - label
% #2 - property
% #3 - tokens in case label and property are available
% #4 - tokens in case label undefined or property not available
\zref@ifrefundefined{#1}{%
\zref@refused{#1}#4%
}{%
\zref@ifrefcontainsprop{#1}{#2}{#3}{%
\protect\G@refundefinedtrue
\@latex@warning{%
Missing property `#2' in reference `#1' on page \thepage
}%
#4%
}%
}%
}%
}%
\zref@newprop{blbt}{false}%
\zref@newprop{blvd}{false}%
\newcommand\blvdANDblbtLABEL[3]{%
\zref@setcurrent{blvd}{#1}%
\zref@setcurrent{blbt}{#2}%
\zref@wrapper@immediate{%
\zref@labelbyprops{#3}{blbt,blvd}%
}%
}%
%%%=======================
\newenvironment{bt}[1]{%
\blvdANDblbtLABEL{false}{true}{#1}%
% Here you could place code that forks
% via \ifbool{\zref@getcurrent{blvd}}{<true>}{<false>}%
% and \ifbool{\zref@getcurrent{blbt}}{<true>}{<false>}%
\par}{}
%%%=======================
\newenvironment{vd}[1]{%
\blvdANDblbtLABEL{true}{false}{#1}%
% Here you could place code that forks
% via \ifbool{\zref@getcurrent{blvd}}{<true>}{<false>}%
% and \ifbool{\zref@getcurrent{blbt}}{<true>}{<false>}%
\par}{}
%%%=======================
\newcommand{\classify}[1]{The below environment is
\@zrefpropdefined{#1}{blvd}{%
\@zrefpropdefined{#1}{blbt}{\@firstoftwo}{\@secondoftwo}%
}{%
\@zrefpropdefined{#1}{blbt}{}{}%
\@secondoftwo
}%
{%
\ifbool{\zref@extractdefault{#1}{blvd}{}}{Example}{%
\ifbool{\zref@extractdefault{#1}{blbt}{}}{Problem}{Wrong}%
}%
}{%
\nfss@text{\reset@font\bfseries??}%
}%
}%
\makeatother
\begin{document}
{\bfseries\classify{Label Of This Environment}}
\begin{bt}{Label Of This Environment}
Contents PPP
\end{bt}
{\bfseries\classify{Label Of That Environment}}
\begin{vd}{Label Of That Environment}
Contents EXEXEX
\end{vd}
\end{document}
(如果现实生活场景也只涉及两个环境,则可以使用单个布尔值来执行“如果\classify
可以省略-command 的错误大小写”。)
使用参考值 你可能根本不需要布尔值:
\documentclass[12pt]{article}
\usepackage{zref}
\makeatletter
\@ifdefinable\@zrefpropdefined{%
\ZREF@Robust\def\@zrefpropdefined{%
\zref@wrapper@babel\@ZREFpropdefined
}%
}%
\@ifdefinable\@ZREFpropdefined{%
\def\@ZREFpropdefined#1#2#3#4{%
% #1 - label
% #2 - property
% #3 - tokens in case label and property are available
% #4 - tokens in case label undefined or property not available
\zref@ifrefundefined{#1}{%
\zref@refused{#1}#4%
}{%
\zref@ifrefcontainsprop{#1}{#2}{#3}{%
\protect\G@refundefinedtrue
\@latex@warning{%
Missing property `#2' in reference `#1' on page \thepage
}%
#4%
}%
}%
}%
}%
\zref@newprop{environmentphrase}{unknown}%
\newcommand\environmentphraseLABEL[2]{%
\zref@setcurrent{environmentphrase}{#1}%
\zref@wrapper@immediate{%
\zref@labelbyprops{#2}{environmentphrase}%
}%
}%
%%%=======================
\newenvironment{bt}[1]{%
\environmentphraseLABEL{Problem}{#1}%
\par}{}
%%%=======================
\newenvironment{vd}[1]{%
\environmentphraseLABEL{Example}{#1}%
\par}{}
%%%=======================
\newcommand{\classify}[1]{The below environment is
\zref@extractdefault{#1}{environmentphrase}{%
\@zrefpropdefined{#1}{environmentphrase}{%
\protect\G@refundefinedtrue
\@latex@warning{%
Unspecified problem in reference `#1' on page \thepage
}%
}{}%
\nfss@text{\reset@font\bfseries??}%
}%
}%
\makeatother
\begin{document}
{\bfseries\classify{Label Of This Environment}}
\begin{bt}{Label Of This Environment}
Contents PPP
\end{bt}
{\bfseries\classify{Label Of That Environment}}
\begin{vd}{Label Of That Environment}
Contents EXEXEX
\end{vd}
\end{document}
对于这两个示例,您需要两个 LaTeX 编译(在两次运行之间无需删除辅助文件)以使所有内容匹配。
这两个示例生成的 .pdf 文件如下所示:
答案3
\documentclass[12pt]{article}
\newif\ifblbt
\newif\ifblvd
\newenvironment{bt}
{\blvdfalse\blbttrue{\bfseries\classify\par}}
{}
\newenvironment{vd}
{\blvdtrue\blbtfalse{\bfseries\classify\par}}
{}
\newcommand\classify{The below environment is \ifblvd Example\else\ifblbt Problem\else Wrong\fi\fi}
\begin{document}
\begin{bt}
Contents PPP
\end{bt}
\begin{vd}
Contents EXEXEX
\end{vd}
\end{document}
答案4
根据@modnar的建议。非常感谢你的帮助!
\documentclass[12pt]{article}
\usepackage{etoolbox}
\newbool{blbt}
\newbool{blvd}
\newif\ifoutput
\newcommand{\classify}{\outputtrue}
\newcommand{\classifyp}{
\ifoutput{
The environment below is
\ifbool{blvd}{Example}{\ifbool{blbt}{Problem}{Wrong}}
}\fi
}
%%%=======================
\newenvironment{bt}{\global\setbool{blvd}{false}\global\setbool{blbt}{true}{\bfseries\classifyp}\par}{}
%%%=======================
\newenvironment{vd}{\global\setbool{blvd}{true}\global\setbool{blbt}{false}{\bfseries\classifyp}\par}{}
%%%=======================
\begin{document}
\classify
\begin{bt}
Contents PPP
\end{bt}
\begin{vd}
Contents EXEXEX
\end{vd}
\begin{bt}
Contents PPP
\end{bt}
\end{document}