0

I am trying to use mini-page to centre floats of various widths. I wish to define the width of the centre page and to calculate the width of the left and right pages

my calculations work correctly, but the numbers are not recognised in the min-page macro

\documentclass[a4paper,11pt]{article}
\usepackage{ifthen}

\newcommand{\mywidth}[1]{\ifthenelse{\equal{#1}{0.6}}{0.2}
                        {\ifthenelse{\equal{#1}{0.7}}{0.15}
                                {\ifthenelse{\equal{#1}{0.8}}{0.1}{0.1}}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\mypage}[2]{
    \begin{minipage}{\mywidth{#1}\textwidth}
    \end{minipage}
    \begin{minipage}{#1\textwidth}
        #2
    \end{minipage}
    \begin{minipage}{\mywidth{#1}\textwidth}
    \end{minipage}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

    \mywidth{0.6}

    \mywidth{0.7}

    \mywidth{0.8}

    \mypage{0.6}{Bye!}

\end{document}
  • You could use etoolbox instead of ifthen and compare dimensions (which, however, need to have some sort of unit). Try \newcommand{\mywidth}[1]{\ifdimequal{#1pt}{0.6pt}{0.2}{\ifdimequal{#1pt}{0.7pt}{0.15}{\ifdimequal{#1pt}{0.8pt}{0.1}{0.1}}}}. I don’t really understand you construction though? – Jasper Habicht Sep 15 '17 at 16:42
  • @JasperHabicht Thanks I am simply using a proportion of the \textwidth: if the centre minipage is 0.6 of the \textwidth, then the left and right minipages will be 0.2 of the \textwidth etc. – Psionman Sep 15 '17 at 16:55
6

\ifthenelse does not work by expansion, and minipage needs a length, that is something that expands to a tex primitive length specification.

Also you had extra spaces in your macro so the total width was more than textwidth.

This is simpler and uses expandable calculations.

\documentclass[a4paper,11pt]{article}
\usepackage{ifthen}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\mypage}[2]{%<<<<
    \begin{minipage}{\dimexpr(\textwidth-#1\textwidth)/2\relax}
    \end{minipage}%<<<<
    \begin{minipage}{#1\textwidth}
        #2
    \end{minipage}%<<<<
    \begin{minipage}{\dimexpr(\textwidth-#1\textwidth)/2\relax}
    \end{minipage}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}


    \mypage{0.6}{Bye!}

\end{document}

although it isn't clear why you need the two empty boxes on either side.

| improve this answer | |
  • Thanks. That does it. Perhaps I'd better explain my motivation. I have created a new float using the listings package. As some code snippets only contain short lines of code they look silly as listings takes up the whole textwidth and the code is left aligned. This enables me to centre the code. I couldn't figure out ant other way to do it :( – Psionman Sep 16 '17 at 16:50
  • @Psionman but why do you need boxes, the central minipage would center if you put it in a center environment or after \centering, why do you need the calculations and fill the margins with empty boxes? – David Carlisle Sep 16 '17 at 18:13
  • Thanks David. Light bulb moment. See what you mean. Wood and trees syndrome. – Psionman Sep 16 '17 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.