The rules you need to remember about dwarves is that once they reach a specific hunger level (while idle) or a slightly higher hunger level (while working), they will attempt to path from their current location to the closest food that is available for eating. Once they reach that food, they will path again to the nearest location where they can eat the food they just picked up (i.e. a table and chair).
Available food means the food must not be flagged as forbidden or rotten, must not be already reserved for another task (such as cooking, brewing, eating by another dwarf, or be being hauled (or reserved for hauling) from its location to a stockpile, brewery, or kitchen).
The Dwarf Fortress pathing algorithm checks for the nearest free object a dwarf wished to path to (in this case food), but ignores the actual usable path the dwarf must take (due to walls, Z levels, or lack of close stairways) to actually reach that object. This of course leads to some sub-optimal (idiotic) pathing behavior by dwarves, since a more optimal pathing algorithm would actually measure dwarf travel distance rather than Cartesian distance to the object.
An example of idiotic pathing is shown by the following:
A dwarf is located on 'Z level 50' and is standing in a dining room with a free table and chair. Three squares away there is available food on a stockpile. Directly below the dwarf (one Z level below) is an available meal (on 'Z level 49'). The dwarf will attempt to path to the food item directly below him, rather than the one three squares away, since it is 'closer'. If the dwarf can calculate a path to that food directly below him, even if reaching it requires walking to a stairway that is halfway across the map, that is what he will do. Once he has picked up that food, he will then path back to the dining area (assuming no closer table and chair exist), once again walking all the way across the map, so he can eat the food.
Now that you know how a dwarf calculates paths, you can look for certain conditions in your fortress to convert completely idiotic dwarf pathing into merely suboptimal/annoying pathing behavior.
As Antipattern has suggested, placing a meeting area in your main dining room will cause any idle dwarves to gather in that location (even before they are hungry). If there is both a food and booze stockpile (that contains available food and booze) inside or very very near (same Z level is recommended) of that dining area, and (available/free) dining tables and chairs have also been placed in the dining area, any idling dwarves in the meeting area should use them.
Unfortunately, working dwarves will decide to path to the closest free food or booze from wherever they were working when they decide they are hungry or thirsty. Once they path from where they stopped working to the closest food/booze, they will then path from there to the nearest free table to eat/drink it. If your library tables/chairs are closer (as per the pathing algorithm, above) than a dining room table/chair they will use your library to eat rather than your dining room.
Probably the only way to guarantee dwarves won't use your library to eat is to surround the library (above, below and all sides with dining areas (that also include food stockpiles). And even then, your researching dwarves may decide to eat in your library if and when they get hungry (if the closest food stockpile to the library is closer to a free library table than to a free dining room table).