将图表并排摆放,溢出到外边距

将图表并排摆放,溢出到外边距

我想将两个数字并排放置(不是子图),而且,我想让图形溢出到外边距(因为它们对于文本宽度来说太宽了。像这样:

截屏

(黑线界定了页面边框。全页 [pdf]

下面的做法确实很有效:

\begin{figure}[h]
  \setcapwidth{0.6\textwidth}
  \begin{flushright}
    \hspace{-\marginfigwidth}%
    \begin{minipage}[t]{0.6\textwidth}
      \centering
      \includegraphics{gfx-1}
      \caption{Caption 1}
    \end{minipage}%
    \hspace{1cm}%
    \begin{minipage}[t]{0.6\textwidth}
      \centering
      \includegraphics{gfx-2}
      \caption{Caption 2}
      \label{fig:free-lunch}
    \end{minipage}
  \end{flushright}
\end{figure}

不幸的是,有一个问题:这只适用于左手边的页面,因为我通过命令强制将其放入边距\hspace{-\marginfigwidth}(其中\marginfigwidth定义为\marginparwidth + \marginparsep)。

我无法理解必须如何更改才能在所有页面上工作。如果我只是删除hspace右侧页面上的底片,页面就会自动断开。我当然可以将整个页面包装到另一页中,minipage但这样做行不通,因为它不会遵守图形之间的规定\hspace并溢出页面边框。

环境wrapfigure也帮不了我,因为它是为单身的仅限图片。

我怎样才能使此代码独立于页面(左/右)工作,以便它始终溢出到利润?

答案1

使用ifoddpagechangepage包来检查图形在哪一页。需要latex运行两次才能确定正确的页码。如果changepage使用该包,则应使用 选项加载strict,因为否则使用的页码可能并非在所有情况下都正确(当 TeX 读取更多材料以寻找分页符等时)。或者,可以将宏\strictpagecheck放在 之前\checkoddpage

诀窍是使用\makebox[\textwidth],这样框才正式\textwidth长,这也可以避免“Overfull hbox”错误。然后使用第二个可选参数\makebox将其对齐到右侧或左侧:

\documentclass{scrbook}
\usepackage{graphicx}
\usepackage{ifoddpage}
%or: \usepackage[strict]{changepage}
\begin{document}

\begin{figure}[h]
  %\setcapwidth{0.6\textwidth}
  \checkoddpage
  \edef\side{\ifoddpage l\else r\fi}%
  \makebox[\textwidth][\side]{%
    \begin{minipage}[t]{0.59\textwidth}
      \centering
      \includegraphics[width=\textwidth]{gfx-1}
      \caption{Caption 1}
    \end{minipage}%
    \hfill
    \begin{minipage}[t]{0.59\textwidth}
      \centering
      \includegraphics[width=\textwidth]{gfx-2}
      \caption{Caption 2}
      \label{fig:free-lunch}
    \end{minipage}%
  }%
\end{figure}
\end{document}

我有类似这样的示例我的网站. 另请参阅TeX 常见问题解答我从哪里得到这个想法。

答案2

\Figure[Width]{imag1}{cap1}{imag2}{cap2}。宽度默认为 textwidth+marginparwidth。但仍可以使用\marginparsep或任何其他值增加。

\documentclass[ngerman]{book}
\usepackage[demo]{graphicx}

\usepackage{babel,blindtext,ifthen}
\newlength\fullwidth\fullwidth=\textwidth
\advance\fullwidth by \marginparwidth

\newcommand\Figure[5][\fullwidth]{%
  \label{\thefigure:WideFigure}
  \begin{figure}[!htb]%
    \abovecaptionskip=0pt\belowcaptionskip=0pt
    \ifthenelse{\isodd{\pageref{\thefigure:WideFigure}}}{}{\hspace*{-\marginparwidth}}%
    \begin{minipage}{#1}
     \begin{minipage}[b]{0.45\linewidth}\centering
     \includegraphics[width=0.95\linewidth]{#2}
     \caption{#3}
     \end{minipage}\hfill%
     \begin{minipage}[b]{0.45\linewidth}\centering
     \includegraphics[width=0.95\linewidth]{#4}
     \caption{#5}
     \end{minipage}%
  \end{minipage}%
 \end{figure}}


\begin{document}

\blindtext
\Figure{foo}{what a wonderful caption}%
       {foo}{What another wonderful caption}

\blindtext[4]
\Figure{foo}{what a wonderful caption}%
       {foo}{What another wonderful caption}

\end{document}

在此处输入图片描述

答案3

@B3ret:

您可以尝试以下代码。我已经彻底测试了代码的关键定义部分,但没有测试图形包含部分。这留给用户。

\documentclass{article}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage[strict]{changepage}
\usepackage{xkeyval}

% Note: \setkeys from 'xkeyval' strips up to 3 levels of outer braces.

\makeatletter
\newcommand*\ifcmdempty[1]{%
  \@nameuse{@\expandafter\ifx\expandafter\\\@gobble#1.\\%
    second\else first\fi oftwo}%
  {\@nameuse{@\ifx#1\@empty first\else second\fi oftwo}}%
  \@secondoftwo
}
\newcommand\@expandarg[2]{%
  \begingroup
  \protected@edef\reserved@a{\endgroup\unexpanded{#1}{#2}}\reserved@a
}
\newcommand\@expandargnobrace[2]{%
  \begingroup
  \protected@edef\reserved@a{\endgroup\unexpanded{#1}#2}\reserved@a
 }
\newcommand*\ifpdfstrcmp[2]{%
  \@nameuse{@\ifnum\pdfstrcmp{\detokenize{#1}}{\detokenize{#2}}=\z@
    first\else second\fi oftwo}%
}
\newcommand*\makecs[1]{\@expandonce{\csname#1\endcsname}}
\newcommand*\putsubfloat[6]{%
  \begingroup
  \protected@edef\reserved@a{\endgroup
    \ifnum#1=\z@\else\noexpand\subfloat[\makecs{mfbdf@#2}][\makecs{mfbdf@#3}]{\fi
    \noexpand\includegraphics[width=\makecs{mfbdf@#4}\textwidth]{\unexpanded{#5}}%
    \noexpand\label{\makecs{mfbdf@#6}}%
    \ifnum#1=\z@\else}\fi
  }%
  \reserved@a
}
\edef\fullwidth{\the\dimexpr\textwidth+\marginparwidth+\marginparsep}

% \Define@Keys is adapted from the development version of keyreader package.
% The new facilities of keyreader package are more extensive
% than seen here:
\newcommand*\define@keysloop[1]{\@define@keysloop#1,\defkey@nil,}
\def\@define@keysloop#1,{%
  \begingroup\edef\reserved@a{\unexpanded{#1}}%
  \expandafter\endgroup\ifx\reserved@a\defkey@nnil\else
    \define@keysdo{#1}\relax
    \expandafter\@define@keysloop
  \fi
}
\def\defkey@nnil{\defkey@nil}
\newcommand*\@expandonce[1]{\unexpanded\expandafter{#1}}
\newcommand*\Define@Keys{\@testopt\@define@keys{KV}}
\def\@define@keys[#1]#2{\@testopt{\@@define@keys{#1}{#2}}{mp@}}
\def\@@define@keys#1#2[#3]#4{%
  \KV@@sp@def\keyprefix{#1}%
  \KV@@sp@def\keyfamily{#2}%
  \KV@@sp@def\macroprefix{#3}%
  \def\key@vals{}%
  \def\define@keys@do##1=##2=##3\defkey@nil{%
    \expandafter\ifx\expandafter\\\@gobble##1.\\%
      \@latex@error{Empty key name}\@ehc
    \fi
    \define@keys@@do##1/##2//\defkey@nil
  }%
  \def\define@keys@@do##1/##2/##3/##4\defkey@nil{%
    \KV@@sp@def\keyname{##1}%
    \KV@@sp@def\keydefault{##2}%
    \edef\key@vals{%
      \ifcmdempty\key@vals{}{\@expandonce\key@vals,}%
      \@expandonce\keyname=\@expandonce\keydefault
    }%
    \edef\reserved@a{%
      [\keyprefix]{\keyfamily}[\macroprefix]{\keyname}%
      [\@expandonce\keydefault]{\unexpanded{##3}}%
    }%
    \expandafter\ifpdfstrcmp\expandafter{\keydefault}{true}{%
      \expandafter\define@boolkey
    }{%
      \expandafter\ifpdfstrcmp\expandafter{\keydefault}{false}{%
        \expandafter\define@boolkey
      }{%
        \expandafter\define@cmdkey
      }%
    }%
    \reserved@a
  }%
  \long\def\define@keysdo##1{\define@keys@do##1==\defkey@nil}%
  \define@keysloop{#4}%
  \edef\reserved@a{%
    \noexpand\setkeys[\keyprefix]{\keyfamily}{\@expandonce\key@vals}%
  }%
  \reserved@a
}
\Define@Keys[KV]{bigfig}[mfbf@]{pos=!htp,label,shortcaption,relsize=0.95,
  testbool=true/\ifmfbf@testbool\def\testfunc##1{xx##1xx}\fi}
% Parameters: [key-val pairs]{file}{caption}
\newcommand{\bigfigure}[3][]{%
  % Default values:
  \setkeys[KV]{bigfig}{pos=!htp,label=fig:#2,
    shortcaption={{{#3}}},relsize=0.95}%
  % User-provided values:
  \ifx\\#1\\\else\setkeys[KV]{bigfig}{#1}\fi
  \@expandargnobrace{\begin{figure}}{[\mfbf@pos]}%
    \checkoddpage
    \@expandargnobrace{\makebox[\textwidth]}{[\ifoddpage l\else r\fi]}{%
      \begin{minipage}[t]{\fullwidth}%
        \centering
        \includegraphics[width=\mfbf@relsize\textwidth]{#2}%
        \caption[\mfbf@shortcaption]{#3}%
        \label{\mfbf@label}%
      \end{minipage}%
    }% 
  \end{figure}%
}
\Define@Keys[KV]{bigdblfig}[mfbdf@]{pos=!htp,mainrelsize=0.95,
    mainlabel=fig:mainlabel,maincaption,mainshortcaption,
    leftrelsize=0.45,leftlabel=fig:leftlabel,leftcaption,
    leftshortcaption,rightrelsize=0.45,rightlabel=fig:rightlabel,
    rightcaption,rightshortcaption
}
% [key-value pairs]{file-left}{file-right}
\newcommand{\bigdoublefigure}[3][]{%
  % The keyreader package avoids this type of repetition:
  \setkeys[KV]{bigdblfig}{pos=!htp,mainrelsize=0.95,
    mainlabel={fig:#2-#3},maincaption=,mainshortcaption=,
    leftrelsize=0.45,leftlabel={fig:#2-left},leftcaption=,
    leftshortcaption=,rightrelsize=0.45,rightlabel={fig:#2-right},
    rightcaption=,rightshortcaption=
  }%
  \ifx\\#1\\\else\setkeys[KV]{bigdblfig}{#1}\fi
  \@expandargnobrace{\begin{figure}}{[\mfbdf@pos]}%
    \checkoddpage
     \@expandargnobrace{\makebox[\textwidth]}{[\ifoddpage l\else r\fi]}{%  
        \begin{minipage}{\mfbdf@mainrelsize\fullwidth}%
          \centering 
          \ifcmdempty\mfbdf@leftcaption{%
            \putsubfloat\z@{}{}{leftrelsize}{#2}{leftlabel}%
          }{%
            \ifcmdempty\mfbdf@leftshortcaption{%
              \putsubfloat\@ne{leftcaption}{leftcaption}{leftrelsize}{#2}{leftlabel}%
            }{%
              \putsubfloat\@ne{leftshortcaption}{leftcaption}{leftrelsize}{#2}{leftlabel}%
            }%
          }%
          \hfill
          \ifcmdempty\mfbdf@rightcaption{%
            \putsubfloat\z@{}{}{rightrelsize}{#3}{rightlabel}%
          }{%
            \ifcmdempty\mfbdf@rightshortcaption{%
              \putsubfloat\@ne{rightcaption}{rightcaption}{rightrelsize}{#3}{rightlabel}%
            }{%
              \putsubfloat\@ne{rightshortcaption}{rightcaption}{rightrelsize}{#3}{rightlabel}%
            }%
          }%
          \ifcmdempty\mfbdf@maincaption{}{%
            \ifcmdempty\mfbdf@mainshortcaption{%
              \caption[\mfbdf@maincaption]{\mfbdf@maincaption}%
            }{%
              \caption[\mfbdf@mainshortcaption]{\mfbdf@maincaption}%
            }%
          }%
          \label{\mfbdf@mainlabel}%
        \end{minipage}
    }%
  \end{figure}
}
\makeatother

\begin{document}

\lipsum[1-2]

\end{document}

答案4

我遇到了同样的问题,但此外我想集成 subfig 包,并且我需要将其放入外部命令中,因为我使用不同的布局(其中一个没有边距)。

我的 TeX-Macro 技能并不强大,因此宏非常大 ;-) 但它对我有用(目前)。

\newlength\fullmarginwidth
\fullmarginwidth=\marginparwidth
\advance\fullmarginwidth by \marginparsep

\newlength\fullwidth
\fullwidth=\textwidth
\advance\fullwidth by \fullmarginwidth

%Parameters: [keyvals (see below)]{file}{caption}
\makeatletter
  \define@key[Marfig]{bigfig}{pos}{\def\MfBfPos{#1}}
  \define@key[Marfig]{bigfig}{relsize}{\def\MfBfRelSize{#1}}
  \define@key[Marfig]{bigfig}{label}{\def\MfBfLabel{#1}}
  \define@key[Marfig]{bigfig}{shortcaption}{\def\MfBfShortCaption{#1}}
\makeatother
\newcommand{\bigfigure}[3][]{%
  \begingroup
    \setkeys[Marfig]{bigfig}{ pos=!htp,
                              label={fig:#2},
                              shortcaption={#3},
                              relsize=0.95}
    \setkeys[Marfig]{bigfig}{#1}
    \def\efigure{\begin{figure}}%
    \expandafter\efigure\expandafter[\MfBfPos]
      \checkoddpage
      \edef\side{\ifoddpage l\else r\fi}
      \makebox[\textwidth][\side]{% 
        \begin{minipage}[t]{\fullwidth}
          \centering
          \includegraphics[width=\MfBfRelSize\textwidth]{#2}
          \caption[\MfBfShortCaption]{#3}
          \label{\MfBfLabel}
        \end{minipage}
      }%
    \end{figure}
  \endgroup
}

\makeatletter
  \define@key[Marfig]{bigdblfig}{pos}{\def\MfBDfPos{#1}}
  \define@key[Marfig]{bigdblfig}{mainrelsize}{\def\MfBDfMainRelSize{#1}}
  \define@key[Marfig]{bigdblfig}{mainlabel}{\def\MfBDfMainLabel{#1}}
  \define@key[Marfig]{bigdblfig}{maincaption}{\def\MfBDfMainCaption{#1}}
  \define@key[Marfig]{bigdblfig}{mainshortcaption}{\def\MfBDfMainShortCaption{#1}}
  \define@key[Marfig]{bigdblfig}{leftrelsize}{\def\MfBDfLeftRelSize{#1}}
  \define@key[Marfig]{bigdblfig}{leftlabel}{\def\MfBDfLeftLabel{#1}}
  \define@key[Marfig]{bigdblfig}{leftcaption}{\def\MfBDfLeftCaption{#1}}
  \define@key[Marfig]{bigdblfig}{leftshortcaption}{\def\MfBDfLeftShortCaption{#1}}
  \define@key[Marfig]{bigdblfig}{rightrelsize}{\def\MfBDfRightRelSize{#1}}
  \define@key[Marfig]{bigdblfig}{rightlabel}{\def\MfBDfRightLabel{#1}}
  \define@key[Marfig]{bigdblfig}{rightcaption}{\def\MfBDfRightCaption{#1}}
  \define@key[Marfig]{bigdblfig}{rightshortcaption}{\def\MfBDfRightShortCaption{#1}}
\makeatother


%Parameters: [keyvals (see below)]{file-left}{file-right}
\newcommand{\bigdoublefigure}[3][]{%
  \begingroup
    %Default values:
    \setkeys[Marfig]{bigdblfig}{pos=!htp,
                                mainrelsize=0.95,
                                mainlabel={fig:#2-#3},
                                maincaption={},
                                mainshortcaption={},%
                                leftrelsize=0.45,
                                leftlabel={fig:#2-left},
                                leftcaption={},
                                leftshortcaption={},%
                                rightrelsize=0.45,
                                rightlabel={fig:#2-right},
                                rightcaption={},
                                rightshortcaption={}}
    %User provided values:
    \setkeys[Marfig]{bigdblfig}{#1}
    \def\efigure{\begin{figure}}%
    \expandafter\efigure\expandafter[\MfBDfPos]
      %Check on which side whe are (right or left)
      \checkoddpage
      \edef\side{\ifoddpage l\else r\fi}
      %Ensure there will be no overfull box message
      \makebox[\textwidth][\side]{% 
        \begin{minipage}{\MfBDfMainRelSize\fullwidth}
          \centering
          \ifthenelse{\equal{\MfBDfLeftCaption}{}}%
          {
            %left subfloat without captions
            \subfloat{%
              \includegraphics[width=\MfBDfLeftRelSize\textwidth]{#2}
              \label{\MfBDfLeftLabel}
            }          
          }
          {
            \ifthenelse{\equal{\MfBDfLeftCaption}{}}%
            {
              %left subfloat with same captions
              \subfloat[\MfBDfLeftCaption][\MfBDfLeftCaption]{%
                \includegraphics[width=\MfBDfLeftRelSize\textwidth]{#2}
                \label{\MfBDfLeftLabel}
              }
            }
            {
              %left subfloat with different captions
              \subfloat[\MfBDfLeftShortCaption][\MfBDfLeftCaption]{%
                \includegraphics[width=\MfBDfLeftRelSize\textwidth]{#2}
                \label{\MfBDfLeftLabel}
              }
            }
          }
          \hfill
           \ifthenelse{\equal{\MfBDfRightCaption}{}}%
          {
            %right subfloat without captions
            \subfloat{%
              \includegraphics[width=\MfBDfRightRelSize\textwidth]{#3}
              \label{\MfBDfRightLabel}
            }
          }
          {
            \ifthenelse{\equal{\MfBDfRightShortCaption}{}}%
            {
              %right subfloat with same captions
              \subfloat[\MfBDfRightCaption][\MfBDfRightCaption]{%
                \includegraphics[width=\MfBDfRightRelSize\textwidth]{#3}
                \label{\MfBDfRightLabel}
              }
            }
            {
              %right subfloat with different captions
              \subfloat[\MfBDfRightShortCaption][\MfBDfRightCaption]{%
                \includegraphics[width=\MfBDfRightRelSize\textwidth]{#3}
                \label{\MfBDfRightLabel}
              }
            }
          }
          \ifthenelse{\equal{\MfBDfMainCaption}{}}
          {
            %no main caption
          }
          {
            \ifthenelse{\equal{\MfBDfMainShortCaption}{}}
            {
              %same main captions
              \caption[\MfBDfMainCaption]{\MfBDfMainCaption}
            }
            {
              %different main captions
              \caption[\MfBDfMainShortCaption]{\MfBDfMainCaption}
            }
          }
          \label{\MfBDfMainLabel}
        \end{minipage}
      }
    \end{figure}
  \endgroup
}

相关内容