We present a method for the recognition of on-line handwritten commutative diagrams. Diagrams are formed with arrows that join relatively simple mathematical expressions. Diagram recognition consists in grouping isolated symbols into simple expressions, recognizing the arrows that join such expressions, and finding the layout that best describes the diagram. We model the layout of the diagram with a grid that optimally fits a tabular arrangement of expressions. Our method maximizes a linear function that measures the quality of our layout model. The recognition results are translated into the LaTeX library xy-pic.