TransWikia.com

parbox automatic width calculation

TeX - LaTeX Asked on August 4, 2021

Consider the following MWE:

documentclass[letterpaper]{article}
usepackage{amsmath}
begin{document}
noindent
$begin{aligned}[t]
  left( frac{1}{2}-frac{1}{4} right) left( frac{1}{2}-frac{3}{4} right) 
  &= left( frac{4-2}{8} right) left( frac{4-6}{8} right) 
  & & parbox[t]{0.5textwidth}{Simplify within parenthesis. 
          Simplify within parenthesis. Simplify within parenthesis. 
          Simplify within parenthesis.} 
  &= left( frac{2}{8} right) left( frac{-2}{8} right) 
  & &parbox[t]{0.5textwidth}{Add within grouping symbols.} 
  &= left( frac{1}{4} right) left(-frac{1}{4} right) 
  & & parbox[t]{0.5textwidth}{Multiply.}
  &=  -frac{1}{16}
end{aligned}$
end{document}

issues the error

enter image description here

This is primarily due to the parbox[t]{0.5textwidth}{<content>}.

Is it possible to create an environment that calculates the remaining linewidth for each line independently that the parbox can use?

enter image description here


Consider the MWE using the linegoal package suggested by @zarko:

documentclass[letterpaper]{article}
usepackage{geometry}
usepackage{mathtools}
usepackage{linegoal}

%---------------- show page layout. don't use in a real document!
usepackage{showframe}
renewcommandShowFrameLinethickness{0.15pt}
renewcommand*ShowFrameColor{color{red}}
%---------------------------------------------------------------%

begin{document}

    begin{alignat*}{2}    
left( frac{1}{2} - frac{1}{4} right) left( frac{1}{2} - frac{3}{4} right)
    & = left( frac{4 - 2}{8} right) left( frac{4 - 6}{8} right) 
        &qquad     
            &   rlap{parbox{linegoal}{raggedright
                Simplify within parenthesis. Simplify within parenthesis.}}
                                                                    [1ex]
    & = left( frac{2}{8} right) left( frac{-2}{8} right)        
        &   &  rlap{parbox{linegoal}{raggedright
                Add within grouping symbols}}                       
    & = left( frac{1}{4} right) left(-frac{1}{4} right)         
        &   & rlap{parbox{linegoal}{Multiply}}                   
    & = -frac{1}{16}
    end{alignat*}
        begin{alignat*}{2}    
left( frac{1}{2} - frac{1}{4} right) left( frac{1}{2} - frac{3}{4} right)
    & = left( frac{4 - 2}{8} right) left( frac{4 - 6}{8} right) 
        &qquad     
            &   rlap{parbox{linegoal}{raggedright
                Simplify within parenthesis. Simplify within parenthesis.}}
                                                                    [1ex]
    & = left( frac{2}{8} right) left( frac{-2}{8} right)        
        &   &  rlap{parbox{linegoal}{raggedright
                Add within grouping symbols}}                       
    & = left( frac{1}{4} right) left(-frac{1}{4} right)         
        &   & rlap{parbox{linegoal}{Multiply}}                   
    & = -frac{1}{16}
    end{alignat*}
end{document}

it issues the warnings shown below:

enter image description here

Is there a fix to this?

2 Answers

With use of linegoal (for parboxes widths) and nccmath (for local moving equation to the left):

documentclass[letterpaper]{article}
usepackage{geometry}
usepackage{nccmath}  
usepackage{linegoal}

%---------------- show page layout. don't use in a real document!
usepackage{showframe}
renewcommandShowFrameLinethickness{0.15pt}
renewcommand*ShowFrameColor{color{red}}
%---------------------------------------------------------------%

begin{document}

begin{fleqn}[4em]
[
begin{aligned}
left( frac{1}{2} - frac{1}{4} right) left( frac{1}{2} - frac{3}{4} right)
    & = left( frac{4 - 2}{8} right) left( frac{4 - 6}{8} right) 
        &qquad     
            &   rlap{parbox{linegoal}{raggedright
                Simplify within parenthesis. Simplify within parenthesis.}}
                                                                    [1ex]
    & = left( frac{2}{8} right) left( frac{-2}{8} right)        
        &   &  rlap{parbox{linegoal}{raggedright
                Add within grouping symbols}}                       
    & = left( frac{1}{4} right) left(-frac{1}{4} right)         
        &   & rlap{parbox{linegoal}{Multiply}}                   
    & = -frac{1}{16}
    end{aligned}
]
end{fleqn}

end{document}

Edit: After first compilation above MWE gives number of warnings, mostly regarding to undefined references. These warnings disappear after second compilation when the parbox get final size:

enter image description here

(red lines indicate text borders)

Answered by Zarko on August 4, 2021

Guessing the width of a p column is the main motivation for the tabularx package:

enter image description here

documentclass{article}

usepackage{tabularx}
usepackage{amsmath}

begin{document}


begin{flushleft}
  begin{tabularx}{linewidth}{
@{}
>{$displaystyle}r<{$}
@{}
>{$displaystyle{}}l<{$}
>{raggedrightarraybackslash}X
@{}
}
  left( frac{1}{2}-frac{1}{4} right) left( frac{1}{2}-frac{3}{4} right) 
  &= left( frac{4-2}{8} right) left( frac{4-6}{8} right) 
  & Simplify within parenthesis. 
          Simplify within parenthesis. Simplify within parenthesis. 
          Simplify within parenthesis. [12pt]
  &= left( frac{2}{8} right) left( frac{-2}{8} right) 
  & Add within grouping symbols. [12pt]
  &= left( frac{1}{4} right) left(-frac{1}{4} right) 
  & Multiply.[12pt]
  &=  -frac{1}{16}
  end{tabularx}
end{flushleft}

end{document}

Answered by David Carlisle on August 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP