我的问题
我怎样才能改善这一点\newcommand
,使我能够
- 模式中带有标题的居中图形
article
- 以标题为中心的图表
beamer
- 的标题
beamer
是article
%------------ %0= Article 1= beamer --------------
\def\model{1}
%----------------load the documentclass ----------------
\if 0\model
\documentclass{article}
\usepackage[envcountsect]{beamerarticle}
\else
\documentclass{beamer}
\fi
\usepackage{tikz}
\usepackage{graphicx}
\newcommand{\hackGraph}[3]{
\if 0\model
\begin{figure}[htp]
\centering
#1
\caption{#3}
\end{figure}
\else
\begin{frame}{#3}
\centering
#2
\end{frame}
\fi
}
\begin{document}
\hackGraph
{\includegraphics[width=0.5\textwidth]{example-image}}
{\includegraphics[width=0.8\textwidth]{example-image}}
{Figure with caption or frame with title}
\end{document}
语境
我使用beamer
和beameraticle
来生成笔记或幻灯片。遵循“投影仪中无数字”原则,我\hackGraph
为所有includegraphics
和创建了\input{TikZ.tex}
例如
\hackGraph
{\resizebox{0.5\textwidth}{!}{\input{TikZ.tex}}}
{\resizebox{0.8\textwidth}{!}{\input{TikZ.tex}}}
{TikZ figure with caption or frame with title}
也可以用于包装/调整我的图形大小TikZ
。
但我在使用过程中发现我做的事情并不正确。
代码似乎重复太多了。如何改进?
答案1
虽然有点晚了,但我一个月前才在搜索类似内容时发现了您的问题。从那时起,我便找到了自己想要的东西,我相信它符合您的需求/要求。
对我来说,重要的是我不会定义自己的开关,而是使用mode<presentation>{}
或mode<article>{}
命令(因为它们已经可用)。但由于这些命令由 beamer 提供,因此它们在文章模式下不起作用,直到您加载\usepackage{beamerarticle}
。换句话说,您不能使用mode<article>{\usepackage{beamerarticle}}
。但是,您可以使用:
\@ifclassloaded{article}{\usepackage{beamerarticle}}
实际上,这可以成为一个条件语句,确保beamerarticle
仅当您将documentclass
文章排版时才加载。
虽然我知道在 beamer 中不应使用图形环境,但我实际上需要在文章模式下使用它,并且(由于[H]
软件包提供的选项float
)它对我来说效果很好 - 在两种模式下都很好。此外(类似于您所寻找的),对我来说,拥有一个可以同时生成演示文稿和完整文章(具有适当的图形环境)的单个 .tex 文件非常重要。
要在其他地方使用图形标题(例如框架标题),可以使用cleveref
包(根据这个帖子)。
%%%%%%%%%%%%%%%%%%%%
% Create more complete clever ref to use
% figure label for auto generation frame title
%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
\let\Chyperref\Cref % Save original command under a new name
\renewcommand{\Cref}[1]{\hyperlink{#1}{\Chyperref{#1}}} % Redefine \cref command and explicitly add the hyperlink.
\newcommand*{\fullref}[1]{\Cref{#1}:~\nameref{#1}}
}
%%%%%%%%%%%%%%%%%%%%
\mode<article>{
\newcommand*{\fullref}[1]{\hyperref[{#1}]{\Cref*{#1}: \nameref*{#1}}}
}
%%%%%%%%%%%%%%%%%%%%
因为超链接在 beamer 模式下与文章模式下略有不同,所以它是单独定义的(这样,您还可以根据需要对每个单独的输出版本进行调整)。
现在您可以使用包captionsetup
中的命令caption
使标准图形标题消失。
\captionsetup[figure]{
labelformat=empty,
textformat=empty,
}
如果需要,您可以针对每个模式进行设置(例如,\mode<presentation>{\captionsetup[figure]{...}}
如果您只想调整 beamer 中的标题,请使用)。同样,您也可以在整个文档的任何地方更改此设置。
完成这些设置后,您现在可以使用如下框架:
\begin{frame}
\frametitle{\fullref{fig: my label}}
\begin{figure}[H]
\includegraphics{example-image}
\caption{My Caption}
\label{fig: my label}
\end{figure}
\end{frame}
最后,这里有一个完整的最小工作示例,供您参考。请注意,开关%
以第 1 行和第 2 行的形式出现。
完整 MWE:
\documentclass{beamer} % Comment out for article
%\documentclass{article} % Uncomment line for article
%%%%%%%%%%%%%%%%%%%%
% Begin Load Packages %%%%%%%
%%%%%%%%%%%%%%%%%%%%
% Packages needed in article mode
\makeatletter
\@ifclassloaded{article}{
\usepackage{beamerarticle}
\usepackage{graphicx} % Automatically loaded in Beamer, but not in Article mode
\usepackage{hyperref} % Automatically loaded in Beamer, but not in Article mode
}
\makeatother
% Packages needed in both modes
\usepackage{float} % Allows use of figure environment in Beamer (using [H])
\usepackage{caption} % Needed to adjust caption
\usepackage{cleveref} % Needed to adjust cross referencing
%%%%%%%%%%%%%%%%%%%%
% End Load Packages %%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
% Begin Custom Commands %%%%%
%%%%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%%%%%%%%%%%
% Create more complete clever ref to use
% figure label for auto generation frame title
%%%%%%%%%%%%%%%%%%%%
\mode<presentation>{
% Idea stolen from https://tex.stackexchange.com/questions/266080/cleveref-hyperlink-not-working-in-beamer/266109#266109
\let\Chyperref\Cref % Save original command under a new name
\renewcommand{\Cref}[1]{\hyperlink{#1}{\Chyperref{#1}}} % Redefine \cref command and explicitly add the hyperlink.
\newcommand*{\fullref}[1]{\Cref{#1}:~\nameref{#1}}
}
%%%%%%%%%%%%%%%%%%%%
\mode<article>{
\newcommand*{\fullref}[1]{\hyperref[{#1}]{\Cref*{#1}: \nameref*{#1}}}
}
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
% Make the standard caption disappear
%%%%%%%%%%%%%%%%%%%%
% Can be encased in \mode<presentation>{}
% to use standard caption in article mode
%
%\mode<presentation>{
\captionsetup[figure]{
labelformat=empty,
textformat=empty,
}
%}
%
%%%%%%%%%%%%%%%%%%%%
% End Custom Commands %%%%%%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
% Standard fix for 2018 LaTeX release
%%%%%%%%%%%%%%%%%%%%
\makeatletter
\let\@@magyar@captionfix\relax
\makeatother
%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{frame}
\frametitle{\fullref{fig: my label 1}}
\begin{figure}[H]
\centering
\includegraphics[height=6cm]{example-image}
\caption{My First Figure Caption}
\label{fig: my label 1}
\end{figure}
\end{frame}
\mode<article>{
In my workflow, I need the figure caption in the frame title (in beamer)
and above the image (in article). However, this can be easily adjusted
through the ``mode switch''
}
\mode<article>{
\captionsetup[figure]{
labelformat=simple,
textformat=simple,
}}
\begin{frame}
\mode<presentation>{\frametitle{ \fullref{fig: my label 2}}}
\mode<article>{\frametitle{My Second Frame}}
\begin{figure}[H]
\centering
\includegraphics[height=6cm]{example-image}
\caption{My Second Figure Caption}
\label{fig: my label 2}
\end{figure}
\end{frame}
\end{document}
对我来说,这种方法确实很有效,但我很想听听您和社区的反馈,因为我总是对改进或更简单的方法感兴趣。这是我能想到的最简约的版本,但如果有更好的方法,我很想知道并使用。