i am new to adobe illustrator and tried to fit an triangle to a square. The problem is that that is not fitting perfectly. Either the corner are outside or the line is not ending with the square. Is there a way to match them perfectly.
Even if your stroked shapes are perfectly aligned, the corners will extrude and you won't get a perfect fit.
Changing the Align Stroke attribute won't help.
In the following I'll show two different solutions to the problem. There might be more.
Use Round Join
If you can live with the appearance, the easiest solution by far is to simply set the Corner attribute to Round Join. That way all corners are circular and will always fit.
Remake the shape
If you need sharp corners, you can remake your graphics so the outer shape is one closed path and the inner lines are separate paths.
In this case I would cut the horizontal line of the triangle, delete the lower horizontal line of the rectangle, join the two shapes the two places where they meet and then paste the horizontal line in place.
Basic math! The triangle’s edge length needs to be same as one of the square’s/rectangle’s sides.
Draw them with equal length.
The go to outline mode (Ctrl+y). Place them manually over each other. hit (ctrl+y) again to go back into normal mode.