2

I'm sorry if I lack the basics of tex and macro definitions, I only started to get into it.

My concrete question is how to get Custom margins for figure in LaTeX, in particular how to Center figure that is wider than \textwidth. But I want this setting defined in a macro.

So this is why I ask a more general question about macro syntax: how to nest the content of my environment inside a command? I thought I just needed to replace { ... } by \bgroup ... \egroup but my attempt at adapting the "makebox" approach in a \renewenvironment failed:

\let\origfig=\figure
\let\endorigfig=\endfigure

\renewenvironment{figure}[1][]{%
\origfig
\noindent\makebox[\textwidth]\bgroup\centering
}{
\egroup\endorigfig
}

throws

! Missing } inserted.
<inserted text>
                }

P.S: as for this specific float width question, I'll happily use packages that get the job done, like floatrow (but I haven't managed to produce the desired result yet).

P.P.S: I want to redefine the table environment this way as well.


EDIT. I added a minimal example based on @egreg's answer:

\documentclass{article}

% Install package "mwe" to get the example image.
\usepackage{graphicx}

% From: https://tex.stackexchange.com/a/541576
\makeatletter
\expandafter\def\expandafter\@floatboxreset\expandafter{\@floatboxreset\centering}
\makeatother

\begin{document}

% To visualise the text margins:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 

\begin{figure}[h]
\includegraphics[width=1.2\linewidth, height=0.1\textheight]{example-image-a.png}
\end{figure}

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 

\end{document}
5
  • you can not use \bgroup to delimit a macro argument \noindent\makebox[\textwidth]\bgroup can not work (not specific to \makebox) Apr 30, 2020 at 15:42
  • Ok, so do I have to use a package like environ instead? Apr 30, 2020 at 15:53
  • 1
    possibly yes but redefining the float environments is tricky there is a lot going on that can break, there is almost always a better way Apr 30, 2020 at 15:54
  • Apart from that, are you sure you want to add \makebox in this way, it means that you will get no warning if your figures/tables overflow the text block or even the page. Getting warnings if the output is not visible is a feature not a problem to be solved. Apr 30, 2020 at 15:56
  • I think that you should change your title to include (centering wide floats) Apr 30, 2020 at 17:05

4 Answers 4

3

You don't want to do this way, but to hook in the float mechanism itself.

There is currently no user interface for this, so you need some low level code:

\makeatletter
\expandafter\def\expandafter\@floatboxreset\expandafter{\@floatboxreset\centering}
\makeatother

This code in the preamble of your document will make LaTeX do \centering as the last command before examining a float contents. So every figure and table environment will have their contents centered.

On the other hand, it's not so difficult to get used to

\begin{figure}[htp]
\centering

and forget the issue.

2
  • I don't understand, I tried this with \begin{figure}\includegraphics[width=1.3\textwidth, height=1cm]{img}\end{figure}, and the image is still overflowing on the right... Apr 30, 2020 at 16:08
  • @PlasmaBinturong Of course you don't usually want to insert images larger than the text width. Why would you?
    – egreg
    Apr 30, 2020 at 16:57
4

egreg has shown how to add centering but to answer your question about \bgroup.

After a definition such as

\newcommand\zzz[1]{this (#1)}

Then the argument #1 has to be a single token or a group surrounded by explicit { and } (or in general characters with catcode 1 and 2). \bgroup is defined by \let so is an implicit character which may be used for tex grouping but not for undelimited arguments.

\zzz\bgroup zzz\egroup

The argument #1 of \zzz is just the single token \bgroup so it would expand to

 this (\bgroup)zzz\egroup

and typeset as

> this ()zzz

In that case you get no error, but unwanted output. In general the unexpected \bgroup may make arbitrary low level errors with mis-matched groups, depending on the details of the command.

4

The memoir class provides the centerfloat command for centering floats wider than the textwidth.

Try this modified version of your MWE.

% widefloatprob.tex  SE 541571

\documentclass{article}

% Install package "mwe" to get the example image.
\usepackage{graphicx}

\makeatletter
\newcommand*{\centerfloat}{%
  \parindent \z@
  \leftskip \z@ \@plus 1fil \@minus \textwidth
  \rightskip\leftskip
  \parfillskip \z@skip}
\makeatother

\begin{document}

% To visualise the text margins:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 

\begin{figure}[h]
\centerfloat
\includegraphics[width=1.2\linewidth, height=0.1\textheight]{example-image-a.png}
\end{figure}

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 

\end{document}

The \centerfloat can be used for any float that has a known width, such as a tabular (table).

2
  • I take it \hsize=\linewidth=\columnwidth (ignoring the glue), so \caption will fit normally. Apr 30, 2020 at 20:14
  • @JohnKormylo The caption fits within the textwidth. It appears as it would with a normal sized float. May 1, 2020 at 17:54
0

As the \makebox approach requires the figure as an argument, you could parse the figure name as a parameter of a new environment:

\documentclass{article}
\usepackage{graphics}
\usepackage{mwe}       %examplary figure "example-image-a"
\usepackage{blindtext} %examplary text "\blindtext"

\begin{document}
  %definitions of the new environment "fig" 
  \newenvironment{fig}[2][\textwidth]
  {\begin{figure}[htbp]\makebox[\textwidth][c]{\includegraphics[width=#1,height=1cm]{#2}}}
  {\end{figure}}

  %example
  \begin{fig}[1.2\textwidth]{example-image-a}
    \caption{Caption}
    \label{fig:key}
  \end{fig}

  \blindtext

\end{document}

Alternatively you could instead define a new command:

\newcommand{\includegraphicsCenter}[2][]{\makebox[\textwidth][c]{\includegraphics[#1]{#2}}}

\begin{figure}[htbp]
    \includegraphicsCenter[width=1.2\textwidth,height=1cm]{example-image-a}
    \caption{Caption}
    \label{fig:key2}
\end{figure}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .